java 文件读写_FileInputStream_File

12/30/2009来源:Java教程人气:12162

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

public static void main(String arg[]) {
  String fileName = "E:\\share\\test.txt";
  File file = new File(fileName);
  String writeData = "HelloWorld!\r\nnihao我的内存是2G的 ";
  byte[] byteOutData = writeData.getBytes();
  byte[] byteInData = new byte[20];
  int length = 0;
  if (file.isFile()) {
   file.delete();
  }
  try {

   if (file.createNewFile() && file.canWrite()) {
    FileOutputStream fileOutStream = new FileOutputStream(file);
    fileOutStream.write(byteOutData);
    fileOutStream.close();
   }
   if (file.canRead()) {
    FileInputStream fileInStream = new FileInputStream(file);
    while ((length = fileInStream.read(byteInData)) != -1) {
     System.out.PRint(new String(byteInData, 0, lenght));
    }
    fileInStream.close();
   }
  } catch (IOException e) {
   System.out.println("IOException occur");
   e.getMessage();
  }
}
}



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/whb3118/archive/2009/12/29/5099577.aspx