java 文件的读和写
http://blog.csdn.net/jiangxinyu/article/details/7885518/
package inputoutput; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringReader; public class FileTestClass { /** * @author:lixh */ public static void main(String[] args) { try { //文件的读写 // InputStreamReader // FileReader // StringReader // /* File file = new File("c:/file.text"); file.createNewFile(); String string = "小蘑菇"; byte[] bt = new byte[1024]; bt = string.getBytes(); //输出流写文件 FileOutputStream out = new FileOutputStream(file); out.write(bt); out.close(); //输入流读取文件in FileInputStream in = new FileInputStream(file); InputStreamReader imrReader = new InputStreamReader(in); int ch = 0; while((ch = imrReader.read()) != -1){ System.out.println((char)ch); }*/ //以字节方式读取文件 // /* File file = new File("c:/file.txt"); FileInputStream in = new FileInputStream(file); int ch; while((ch = in.read())!= -1){ System.out.println("ch--"+ch); }*/ //以字符方式读取文件 /*File file = new File("c:/file.txt"); InputStreamReader in = new InputStreamReader(new FileInputStream(file)); int ch; while((ch = in.read()) != -1){ System.out.println((char)ch); }*/ //逐行读取文件 File file = new File("c:/file.txt"); BufferedReader fileReader = new BufferedReader(new FileReader(file)); String line = null; while( (line = fileReader.readLine()) != null){ System.out.println(line); } //随机读取文件 //创建文件 /*File file = new File("c:/test.txt"); if (!file.exists()) { file.createNewFile(); }*/ //创建文件夹 /* File file = new File("c:/test"); if (!file.exists()) { file.mkdir(); // file.mkdirs(); }*/ } catch (Exception e) { e.printStackTrace(); } } }