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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

1packagesrc;23importjava.io.File;4importjava.io.FileNotFoundException;5importjava.io.FileReader;6importjava.io.IOException;78publicclassTest {910publicstaticvoidmain(String[] args) {11File f =newFile("src\\test1.txt");//文件在src名为test1.txt中12try{13FileReader fr =newFileReader(f);//将文件读取到内容中14intm;15while((m=fr.read())!=-1){16System.out.PRint((char)(m));17}18}catch(FileNotFoundException e) {19//TODO Auto-generated catch block20e.printStackTrace();21}catch(IOException e) {22//TODO Auto-generated catch block23e.printStackTrace();24}25}26}

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

1packagesrc;23importjava.io.File;4importjava.io.FileInputStream;5importjava.io.FileNotFoundException;6importjava.io.FileOutputStream;7importjava.io.IOException;89publicclassTest {1011publicstaticvoidmain(String[] args) {12File f1 =newFile("src\\fin.txt");//src下fin.txt文件13File f2 =newFile("src\\fout.txt");//src下fout.txt文件1415try{16FileInputStream fis =newFileInputStream(f1);17FileOutputStream fos =newFileOutputStream(f2);1819byte[] temp =newbyte[80];//定义一个字节数组20fis.read(temp);//读到内存中21fos.write(temp);//写到文件2223fis.close();24fos.close();25}catch(FileNotFoundException e) {26//TODO Auto-generated catch block27e.printStackTrace();28}catch(IOException e) {29//TODO Auto-generated catch block30e.printStackTrace();31}3233}34}

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

1packagesrc;23importjava.io.BufferedReader;4importjava.io.BufferedWriter;5importjava.io.File;6importjava.io.FileNotFoundException;7importjava.io.FileReader;8importjava.io.FileWriter;9importjava.io.IOException;1011publicclassTest {1213publicstaticvoidmain(String[] args) {14File f1 =newFile("src\\fin.txt");//src下fin.txt文件15File f2 =newFile("src\\fout.txt");//src下fout.txt文件1617try{18FileReader fr =newFileReader(f1);19FileWriter fw =newFileWriter(f2);2021BufferedReader br =newBufferedReader(fr);22BufferedWriter bw =newBufferedWriter(fw);2324String temp;25inti=1;26while((temp=br.readLine())!=null){27bw.write(i+","+temp);28bw.newLine();//换行29i++;30}31bw.flush();//把缓冲区内容写到文件32

br.close();33bw.close();34

br.close();35bw.close();36}catch(FileNotFoundException e) {37//TODO Auto-generated catch block38e.printStackTrace();39}catch(IOException e) {40//TODO Auto-generated catch block41e.printStackTrace();42}43}44}

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

1packagesrc;23importjava.io.File;4importjava.io.FileWriter;5importjava.io.IOException;6importjava.util.Scanner;78publicclassTest {910publicstaticvoidmain(String[] args) {11File f =newFile("src\\input.txt");12try{13FileWriter fw =newFileWriter(f);14Scanner scanner =newScanner(System.in);15String temp;16while(!((temp=scanner.nextLine()).equals("quit"))){17fw.write(temp);18}19fw.close();20}catch(IOException e) {21//TODO Auto-generated catch block22e.printStackTrace();23}24}25}

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

1packagesrc;23importjava.io.File;4importjava.io.FileNotFoundException;5importjava.io.FileReader;6importjava.io.FileWriter;7importjava.io.IOException;8publicclassTest {910publicstaticvoidmain(String[] args) {11File f1=newFile("src\\fin.txt");12File f2=newFile("src\\fout.txt");13try{14FileReader fr=newFileReader(f1);15FileWriter fw=newFileWriter(f2);1617chartemp[]=newchar[40];18intlen;19while((len=fr.read(temp))!=-1)20{21if(len==40)22fw.write(newString(temp)+"\n");23else24fw.write(temp, 0, len);25}26fr.close();27fw.close();2829}catch(FileNotFoundException e) {30//TODO 自动生成的 catch 块31e.printStackTrace();32}catch(IOException e) {33//TODO 自动生成的 catch 块34e.printStackTrace();35}36}37}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航