字符流
Reader和Writer,這兩個類是抽象類,通常咱們使用的都是他們的子類java
FileReader和FileWriter咱們通常使用的數組
一個小小的例子:ide
package null08051400; import java.io.FileReader; import java.io.IOException; public class Demo01 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub FileReader fileReader = new FileReader("D:\\nullnull\\workspace2\\day03\\src\\null08051400\\new1.txt"); int i; while((i=fileReader.read())!=-1){ System.out.println((char)i); } fileReader.close(); } }
和以前的IO流中的write都是同樣的,這裏咱們直接列出一個例子:編碼
package null08051408; import java.io.FileWriter; import java.io.IOException; public class Demo01 { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub FileWriter writer = new FileWriter("D:\\nullnull\\workspace2\\day03\\src\\null08051408\\1.txt"); writer.write("你好"); writer.write("\r\n"); writer.write("你好"); writer.write("\r\n"); writer.close(); } }
package null08051414; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo01 { public static void main(String[] args) throws IOException { try( FileReader fileReader = new FileReader("D:\\nullnull\\workspace2\\day03\\src\\null08051400\\new1.txt"); FileWriter fileWriter = new FileWriter("D:\\nullnull\\workspace2\\day03\\src\\null08051414\\1.txt"); ){ int i; while((i=fileReader.read())!=-1){ fileWriter.write(i); } } } }
package null08051419; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo01 { public static void main(String[] args) throws IOException { try( FileReader fileReader = new FileReader(""); FileWriter fileWriter = new FileWriter(""); ){ char[] ch = new char[8*1024]; int len; while((len=fileReader.read(ch))!=-1){ fileWriter.write(ch, 0, len); } } } }
使用FileReader讀取字符:先讀字節,把字節轉成字符(byte===>char)spa
使用FileWriter寫入字符:str轉爲字符數組,而後是字符轉爲字節寫入文件code
BufferedReader和BufferedWriter對象
在兩個類的內部有一個緩衝區(字符數組)blog
read方法會讀取8*1024個字符遞歸
write方法會寫入8*1024個字符內存
package null08051558; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo01 { public static void main(String[] args) throws IOException { FileReader fr = new FileReader(""); FileWriter fw = new FileWriter(""); BufferedReader br = new BufferedReader(fr); BufferedWriter bw = new BufferedWriter(fw); int i; while((i=br.read())!=-1){ bw.write(i); } } }
BufferedReader的新方法readLIne()
BufferedWriter的新方法newLine()
package null08051603; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo01 { public static void main(String[] args) throws IOException { FileReader fr = new FileReader(""); FileWriter fw = new FileWriter(""); BufferedReader br = new BufferedReader(fr); BufferedWriter bw = new BufferedWriter(fw); String line = null; while((line=br.readLine())!=null){ bw.write(line); bw.newLine();//換行用 } } }
package null08051607; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Demo01 { public static void main(String[] args) throws IOException { //建立一個集合對象,用於存儲取出的內容 List<String> list = new ArrayList<String>(); //讀取文件寫入集合 BufferedReader br = new BufferedReader(new FileReader("")); String line = null; while((line=br.readLine())!=null){ list.add(line); } //倒敘循環集合,而後輸入 BufferedWriter bw = new BufferedWriter(new FileWriter("")); for(int i=list.size()-1;i>=0;i--){ bw.write(i); bw.newLine(); } br.close(); bw.close(); } }
package null08051614; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; //獲取一個文本中字符出現的次數 public class Demo01 { public static void main(String[] args) throws IOException { Map<Character,Integer> map = new HashMap<Character,Integer>(); FileReader fileReader = new FileReader(""); int i; while((i=fileReader.read())!=-1){ char ch = (char)i; if(map.get(ch)==null){ map.put(ch, 1); }else{ map.put(ch,map.get(ch)+1); } } fileReader.close(); System.out.println(map); } }
InputStreamReader:這個類主要用於指定編碼的讀字符
OutputStreamWriter:這個類主要用於指定編碼的寫入文件
一個小小的例子:
package null08051624; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Demo01 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream(""); FileOutputStream fos = new FileOutputStream(""); InputStreamReader isr = new InputStreamReader(fis,"utf-8"); OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8"); BufferedReader br = new BufferedReader(isr); BufferedWriter bw = new BufferedWriter(osw); String line; while((line=br.readLine())!=null){ bw.write(line); bw.newLine(); } bw.close(); br.close(); } }
一個小小的例子:使用序列流將多個文件寫入到同一個文件中
package null08051633; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; //將兩個文件的內容寫到同一個文件中 public class Demo01 { public static void main(String[] args) throws IOException { //建立輸入流 FileInputStream fis1 = new FileInputStream(""); FileInputStream fis2 = new FileInputStream(""); SequenceInputStream sis = new SequenceInputStream(fis1,fis2); //建立輸出流 FileOutputStream fos = new FileOutputStream(""); int i; while((i=sis.read())!=-1){ fos.write(i); } sis.close(); fis2.close(); fos.close(); } }
package null08051639; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; //多個文件輸入到同一個文件 public class Demo01 { public static void main(String[] args) throws IOException { Vector<InputStream> vector = new Vector<InputStream>(); vector.add(new FileInputStream("")); vector.add(new FileInputStream("")); vector.add(new FileInputStream("")); Enumeration<InputStream> e = vector.elements(); SequenceInputStream sis = new SequenceInputStream(e); FileOutputStream fos = new FileOutputStream(""); int i; while((i = sis.read())!=-1){ fos.write(i); } fos.close(); sis.close(); } }
一個小小的例子:求5的階乘
package null08051649; public class Demo01 { public static void main(String[] args) { int result = s(3); System.out.println(result); } public static int s(int i){ if(i==1){ return 1; }else{ return i*s(i-1); } } }
一個小小的例子:遞歸打印一個目錄下的全部文件
public static void s(File dir){ File[] currentFileList = dir.listFiles(); for(File item:currentFileList){ if(item.isDirectory()){ s(item); }else{ System.out.println(item.getAbsolutePath()); } } }