package MyProject1; /* * *读取文本文件,每次读取一个字节并输出 * */ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; public class Test1 { public static void main(String[] args) throws IOException { FileReader fs=new FileReader(new File("D:/Java/day17worker/123.txt")); char [] buf=new char[20]; int len=fs.read(buf); while(len!=-1){ System.out.println(new String(buf,0,len)); len=fs.read(buf); } fs.close(); } }
package MyProject2; /* * 读取文本文件全部数据,每次读到数据存放到数组中(这道题我自己写的总感觉哪里不对) */ import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { FileInputStream fr=new FileInputStream("D:/Java/day17worker/123.txt"); byte [] buf=new byte[2]; String [] str=new String[100]; int len=fr.read(buf); while(len!=-1){ str[len]=new String(buf,0,len); len=fr.read(buf); } fr.close(); System.out.println(str); // System.out.println(s); } private static char[] s(byte[] buf, int i, int len) { // TODO Auto-generated method stub return null; } }
package MyProject3; /* * 将键盘输入的一串内容,写入到文本文件中 * */ import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class Test { public static void main(String[] args) throws IOException { FileOutputStream fs=new FileOutputStream("D:/Java/day17worker/123.txt",true); System.out.println("请输入要添加的文本"); Scanner sc=new Scanner(System.in); String s=sc.next(); byte [] buf=s.getBytes(); fs.write(buf); fs.close(); System.out.println("输入成功"); } }
package MyProject4; /* * 用FileInputStream和FilerOutputStream实现图片的复制 */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Test4 { public static void main(String[] args) { BufferedInputStream bis=null; BufferedOutputStream bos=null; try { FileInputStream fis=new FileInputStream("D:/Java/day17worker/day17.jpg"); FileOutputStream fos=new FileOutputStream("D:/Java/day17worker/day17002.jpg"); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); int n=bis.read(); while(n!=-1){ bos.write(n); n=bis.read(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(bos!=null){ try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(bis!=null){ try { bis.close(); } catch (IOException e) { // TODO Auto-generated catch block
package MyProject5; /* * 将一个文件复制到另一个文件中(缓冲流) */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Test { public static void main(String[] args) { test1(); } public static void test(){ BufferedInputStream bfs=null; BufferedOutputStream bfo=null; try { FileInputStream fs=new FileInputStream("D:/Java/day17worker/123.txt"); FileOutputStream fo=new FileOutputStream("D:/Java/day17worker/12301.txt",true); bfs = new BufferedInputStream(fs); bfo = new BufferedOutputStream(fo); int s=bfs.read(); while(s!=-1){ bfo.write(s); s=bfs.read(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(bfo!=null) try { bfo.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(bfs!=null){ try { bfs.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void test1(){ BufferedReader br=null; BufferedWriter bw=null; try { FileReader fr=new FileReader("D:/Java/day17worker/123.txt"); FileWriter fw=new FileWriter("D:/Java/day17worker/123010.txt",true); br = new BufferedReader(fr); bw = new BufferedWriter(fw); String s=br.readLine(); while(s!=null){ bw.write(s); bw.newLine();//换行 s=br.readLine(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(br!=null){ try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(bw!=null){ try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
e.printStackTrace();}}}System.out.println("复制成功");}}package MyProject6; /* * 向ArrayList集合中添加3个字符串类型的数据,最后将集合中的内容写入到log.txt文件中 */ import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; public class Test { public static void main(String[] args) throws IOException { ArrayList<String> list=new ArrayList<String>(); list.add("北京"); list.add("上海"); list.add("深圳"); FileWriter fs=new FileWriter("D:/Java/day17worker/log.txt"); BufferedWriter bfs=new BufferedWriter(fs); for(int i=0;i<list.size();i++){ bfs.write(list.get(i)); bfs.newLine(); } bfs.close(); } }
java.io流的初级应用
最新推荐文章于 2023-06-27 12:35:44 发布