国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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 }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 枣强县| 定陶县| 朝阳区| 克拉玛依市| 越西县| 江门市| 墨竹工卡县| 工布江达县| 绿春县| 健康| 通渭县| 沙坪坝区| 青田县| 晋江市| 水富县| 桦南县| 陆河县| 邯郸县| 永嘉县| 通江县| 长宁县| 沽源县| 崇州市| 红原县| 吉木乃县| 克山县| 崇明县| 蒙阴县| 湖口县| 上高县| 南陵县| 沈阳市| 外汇| 桑日县| 梓潼县| 沁水县| 饶平县| 内丘县| 星子县| 清原| 淄博市|