日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java输入输出流实例代码

發(fā)布時間:2023/12/31 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java输入输出流实例代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.編寫一個程序,讀取源代碼文件的內(nèi)容并在控制臺輸出。如果源文件不存在,則顯示相應的錯誤信息。

package src;import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;public class test01 {public static void main(String[] args) {File f = new File("test01.java");//文件當前目錄下,在eclipse下是該工程目錄下。try {FileReader fr = new FileReader(f);//將文件讀取到內(nèi)容中int m;//int包含char的范圍while((m=fr.read())!=-1){System.out.print((char)(m));//強制轉為char}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }



2.編寫一個程序?qū)崿F(xiàn)如下功能,從當前目錄下的文件input.txt中讀取80個字節(jié)(實際讀到的字節(jié)數(shù)可能比80少)并將讀來的字節(jié)寫入當前目錄下的文件output.txt中

package src;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class test01 {public static void main(String[] args) {File f1 = new File("input.txt");File f2 = new File("output.txt");try {FileInputStream fis = new FileInputStream(f1);FileOutputStream fos = new FileOutputStream(f2);byte[] temp = new byte[80];//定義一個字節(jié)數(shù)組fis.read(temp);//讀到內(nèi)存中fos.write(temp);//寫到文件fis.close();fos.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("運行結束");} }
3,使用java的輸入/輸出流技術將一個文本文件的內(nèi)容按行讀出,每讀出一行就順序添加行號,并寫入到另一個文件中。

package src;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;public class test01 {public static void main(String[] args) {File f1 = new File("input.txt"); File f2 = new File("output.txt");try {FileReader fr = new FileReader(f1);FileWriter fw = new FileWriter(f2);BufferedReader br = new BufferedReader(fr);BufferedWriter bw = new BufferedWriter(fw);String temp;int i=1;//行號while((temp=br.readLine())!=null){bw.write(i+","+temp);bw.newLine();//換行i++;}bw.flush();//把緩沖區(qū)內(nèi)容寫到文件,如果沒有這條語句,輸出文件為空。 //使用緩存型流時操作完成后必須加上flush語句。br.close();bw.close();br.close();bw.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("運行結束");} }
4.編寫一個程序,接收從鍵盤輸入的數(shù)據(jù),并把從鍵盤輸入的內(nèi)容寫到input.txt文件中,如果輸入"quit",則程序結束。

package src;import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner;public class Test {public static void main(String[] args) {File f = new File("input.txt");try {FileWriter fw = new FileWriter(f);Scanner scanner = new Scanner(System.in);String temp;while(!((temp=scanner.nextLine()).equals("quit"))){fw.write(temp);}fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }
5.編寫一個程序?qū)崿F(xiàn)如下功能,文件input.txt是無行結構(無換行符)的漢語文件,從input中讀取字符,寫入文件output.txt中,每10個字符一行(最后一行可能少于10個字)

/** 注意設置input.txt為UTF-8格式,否則讀取中文顯示亂碼 */ package src;import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class test01 {public static void main(String[] args) {File f1=new File("input.txt");File f2=new File("output.txt");try {FileReader fr=new FileReader(f1);FileWriter fw=new FileWriter(f2);char temp[]=new char[10];int len;while((len=fr.read(temp))!=-1){if(len==10)fw.write(new String(temp)+"\r\n");/* * windows下的文本文件換行符:\r\n linux/unix下的文本文件換行符:\r * Mac下的文本文件換行符:\n */ elsefw.write(temp, 0, len);}fr.close();fw.close();} catch (FileNotFoundException e) {// TODO 自動生成的 catch 塊e.printStackTrace();} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}System.out.println("程序結束");} }6.逐行讀取漢字文件,復制到另一個文件

package src;import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter;public class test01 {public static void main(String[] args) {File f = new File("input.txt");File outFile=new File("output.txt");InputStreamReader read = null;OutputStreamWriter write=null;try {read = new InputStreamReader (new FileInputStream(f));//注意事先設置好input.txt的編碼格式為UTF-8,否則輸出亂碼write=new OutputStreamWriter(new FileOutputStream(outFile));} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}BufferedReader reader=new BufferedReader(read);String line;try {while ((line = reader.readLine()) != null) {System.out.println(line);write.write(line+"\r\n");}write.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

總結

以上是生活随笔為你收集整理的java输入输出流实例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。