首页 > 学院 > 开发设计 > 正文

java输入输出流(内容练习)

2019-11-14 15:34:35
字体:
来源:转载
供稿:网友

1,编写一个程序,读取文件test.txt的内容并在控制台输出。如果源文件不存在,则显示相应的错误信息。

 1 package src; 2  3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.IOException; 7  8 public class Test { 9 10     public static void main(String[] args) {11         File f = new File("src//test1.txt");//文件在src名为test1.txt中12         try {13             FileReader fr = new FileReader(f);//将文件读取到内容中14             int m;15             while((m=fr.read())!=-1){16                 System.out.PRint((char)(m));17             }18         } catch (FileNotFoundException e) {19             // TODO Auto-generated catch block20             e.printStackTrace();21         } catch (IOException e) {22             // TODO Auto-generated catch block23             e.printStackTrace();24         }25     }26 }

 

2,编写一个程序实现如下功能,从当前目录下的文件fin.txt中读取80个字节(实际读到的字节数可能比80少)并将读来的字节写入当前目录下的文件fout.txt中

 1 package src; 2  3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8  9 public class Test {10 11     public static void main(String[] args) {12         File f1 = new File("src//fin.txt");//src下fin.txt文件13         File f2 = new File("src//fout.txt");//src下fout.txt文件14         15         try {16             FileInputStream fis = new FileInputStream(f1);17             FileOutputStream fos = new FileOutputStream(f2);18             19             byte[] temp = new byte[80];//定义一个字节数组20             fis.read(temp);//读到内存中21             fos.write(temp);//写到文件22             23             fis.close();24             fos.close();25         } catch (FileNotFoundException e) {26             // TODO Auto-generated catch block27             e.printStackTrace();28         } catch (IOException e) {29             // TODO Auto-generated catch block30             e.printStackTrace();31         }32         33     }34 }

 

3,使用java的输入/输出流技术将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。

 1 package src; 2  3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.io.IOException;10 11 public class Test {12 13     public static void main(String[] args) {14         File f1 = new File("src//fin.txt");//src下fin.txt文件15         File f2 = new File("src//fout.txt");//src下fout.txt文件16         17         try {18             FileReader fr = new FileReader(f1);19             FileWriter fw = new FileWriter(f2);20             21             BufferedReader br = new BufferedReader(fr);22             BufferedWriter bw = new BufferedWriter(fw);23             24             String temp;25             int i=1;26             while((temp=br.readLine())!=null){27                 bw.write(i+","+temp);28                 bw.newLine();//换行29                 i++;30             }31             bw.flush();//把缓冲区内容写到文件32             br.close();33             bw.close();34             br.close();35             bw.close();36         } catch (FileNotFoundException e) {37             // TODO Auto-generated catch block38             e.printStackTrace();39         } catch (IOException e) {40             // TODO Auto-generated catch block41             e.printStackTrace();42         }43     }44 }

 

4,编写一个程序,接收从键盘输入的数据,并把从键盘输入的内容写到input.txt文件中,如果输入"quit",则程序结束。

 1 package src; 2  3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.util.Scanner; 7  8 public class Test { 9 10     public static void main(String[] args) {11         File f = new File("src//input.txt");12         try {13             FileWriter fw = new FileWriter(f);14             Scanner scanner = new Scanner(System.in);15             String temp;16             while(!((temp=scanner.nextLine()).equals("quit"))){17                 fw.write(temp);18             }19             fw.close();20         } catch (IOException e) {21             // TODO Auto-generated catch block22             e.printStackTrace();23         }24     }25 }

 

5,编写一个程序实现如下功能,文件fin.txt是无行结构(无换行符)的汉语文件,从fin中读取字符,写入文件fou.txt中,每40个字符一行(最后一行可能少于40个字)

 1 package src; 2  3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 public class Test { 9 10     public static void main(String[] args) {11         File f1=new File("src//fin.txt");12         File f2=new File("src//fout.txt");13         try {14             FileReader fr=new FileReader(f1);15             FileWriter fw=new FileWriter(f2);16             17             char temp[]=new char[40];18             int len;19             while((len=fr.read(temp))!=-1)20             {21                 if(len==40)22                   fw.write(new String(temp)+"/n");23                 else24                     fw.write(temp, 0, len);25             }26             fr.close();27             fw.close();28             29         } catch (FileNotFoundException e) {30             // TODO 自动生成的 catch 块31             e.printStackTrace();32         } catch (IOException e) {33             // TODO 自动生成的 catch 块34             e.printStackTrace();35         }36     }37 }

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表