java的io简单应用 选择自 FrankTaylor 的 Blog

类别:Java 点击:0 评论:0 推荐:
//从文件里读数据,显示到屏幕上
package com.yourcompany.core;

import java.io.*;

public class Think {
 
 public static void main(String[] args) {
  
  try {
   
   FileInputStream file = new FileInputStream("d:\\sss.txt");
   char[] c = new char[file.available()];
   for (int i = 0 ; i<c.length;i++) {
    c[i] = (char)file.read();
    
   }
   System.out.print(c);
   
  }
  catch(FileNotFoundException e) {
   
   e.printStackTrace();
  }
  catch(IOException e) {
   
   e.printStackTrace();
  }
  
 }
}
//在屏幕上写东西到文件中
package com.yourcompany.core;

import java.io.*;
import java.util.*;


public class Think {
 
 public static void main(String[] args) {
  
  try {
   
   byte[] c = new byte[1000] ;
   System.in.read(c);
   FileOutputStream file = new FileOutputStream("d:\\s.txt");
   file.write(c);
  }
  catch(Exception e) {
   e.printStackTrace();
  }
 }
}
//从一个文件复制到另一个文件
package com.yourcompany.core;

import java.io.*;
import java.util.*;


public class Think {
 
 public static void main(String[] args) {
  
  try {
   
   FileInputStream file = new FileInputStream("d:\\sss.txt");
   FileOutputStream fileout = new FileOutputStream("d:\\aaa.txt");
   byte[] c = new byte[file.available()];
   for(int i = 0;i<c.length;i++) {
    c[i] = (byte)file.read();
   }
   fileout.write(c);
  }
  catch(Exception e) {
   e.printStackTrace();
  }
 }
}
//在屏幕上你输入什么我就输入什么
package com.yourcompany.core;

import java.io.*;
import java.util.*;


public class Think {
 
 public static void main(String[] args) {
  
  try {
   
   byte [] c = new byte[1000];
   int cc = System.in.read(c);
   for (int i=0;i<cc;i++) {
    System.out.print((char)c[i]);
   }
  }
  catch(Exception e) {
   e.printStackTrace();
  }
  
 }
}

本文地址:http://com.8s8s.com/it/it12441.htm