java读文件
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 9 public class ReaderTest1 { 10 public static void main(String[] args) throws IOException{ 11 reader(); 12 } 13 public static void reader() throws IOException { 14 //定义一个指向c:\\x.txt的字节流 15 FileInputStream fis = new FileInputStream("C:\\x.txt"); 16 //字节流转换成InputStreamReader 17 InputStreamReader isr = new InputStreamReader(fis); 18 //InputStreamReader 转换成带缓存的bufferedReader 19 BufferedReader bufferedReader = new BufferedReader(isr); 20 //将读取出来的信息赋值给字符串s 21 String ss = new String(); 22 String s; 23 while ((s = bufferedReader.readLine())!=null) { 24 ss += s; 25 } 26 //定义一个指向输出的字节流 27 FileOutputStream fos = new FileOutputStream("D:\\x.txt"); 28 //字节流转换成outputStreamWriter 29 OutputStreamWriter osw = new OutputStreamWriter(fos); 30 //outputStreamWriter 转换成带缓存的bufferedReader 31 BufferedWriter bufferedWriter = new BufferedWriter(osw); 32 bufferedWriter.write(ss); 33 bufferedWriter.close(); 34 osw.close(); 35 fos.close(); 36 37 } 38 }
追加内容
1 import java.io.FileOutputStream; 2 import java.io.IOException; 3 4 public class FileOutputStreamtrueTest { 5 public static void fileInputStreamTest() throws IOException { 6 String filename; 7 filename = "C:\\a.txt"; 8 String hello = "hello java"; 9 FileOutputStream fos = new FileOutputStream(filename,true); 10 byte b[] = hello.getBytes(); 11 fos.write(b); 12 fos.close(); 13 } 14 public static void main(String[] args) throws IOException { 15 fileInputStreamTest(); 16 } 17 }