六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

Java中對(duì)文件的讀寫(xiě)設(shè)置之比較

[摘要]Java 中對(duì)文件的讀寫(xiě)操作之比較 Java 對(duì)文件進(jìn)行讀寫(xiě)操作的例子很多,讓初學(xué)者感到十分困惑,我覺(jué)得有必要將各種方法進(jìn)行 一次分析,歸類(lèi),理清不同方法之間的異同點(diǎn)。 一.在 JDK 1.0 中,通常是用 InputStream & OutputStream 這兩個(gè)基類(lèi)來(lái)進(jìn)行讀寫(xiě)操作的。...
Java 中對(duì)文件的讀寫(xiě)操作之比較

Java 對(duì)文件進(jìn)行讀寫(xiě)操作的例子很多,讓初學(xué)者感到十分困惑,我覺(jué)得有必要將各種方法進(jìn)行
一次分析,歸類(lèi),理清不同方法之間的異同點(diǎn)。

一.在 JDK 1.0 中,通常是用 InputStream & OutputStream 這兩個(gè)基類(lèi)來(lái)進(jìn)行讀寫(xiě)操作的。
InputStream 中的 FileInputStream 類(lèi)似一個(gè)文件句柄,通過(guò)它來(lái)對(duì)文件進(jìn)行操作,類(lèi)似的,在
OutputStream 中我們有 FileOutputStream 這個(gè)對(duì)象。

用FileInputStream 來(lái)讀取數(shù)據(jù)的常用方法是:
FileInputStream fstream = new FileInputStream(args[0]);
DataInputStream in = new DataInputStream(fstream);
用 in.readLine() 來(lái)得到數(shù)據(jù),然后用 in.close() 關(guān)閉輸入流。
完整代碼見(jiàn) Example 1。

用FileOutputStream 來(lái)寫(xiě)入數(shù)據(jù)的常用方法是:
FileOutputStream out out = new FileOutputStream("myfile.txt");
PrintStream p = new PrintStream( out );
用 p.println() 來(lái)寫(xiě)入數(shù)據(jù),然后用 p.close() 關(guān)閉輸入。
完整代碼見(jiàn) Example 2。


二.在 JDK 1.1中,支持兩個(gè)新的對(duì)象 Reader & Writer, 它們只能用來(lái)對(duì)文本文件進(jìn)行操作,而
JDK1.1中的 InputStream & OutputStream 可以對(duì)文本文件或二進(jìn)制文件進(jìn)行操作。

用FileReader 來(lái)讀取文件的常用方法是:
FileReader fr = new FileReader("mydata.txt");
BufferedReader br = new BufferedReader(fr);
用 br.readLing() 來(lái)讀出數(shù)據(jù),然后用br.close() 關(guān)閉緩存,用fr.close() 關(guān)閉文件。
完整代碼見(jiàn) Example 3。

用 FileWriter 來(lái)寫(xiě)入文件的常用方法是:
FileWriter fw = new FileWriter("mydata.txt");
PrintWriter out = new PrintWriter(fw);
在用out.print 或 out.println 來(lái)往文件中寫(xiě)入數(shù)據(jù),out.print 和 out.println的唯一區(qū)別是后者寫(xiě)
入數(shù)據(jù)或會(huì)自動(dòng)開(kāi)一新行。寫(xiě)完后要記得 用out.close() 關(guān)閉輸出,用fw.close() 關(guān)閉文件。
完整代碼見(jiàn) Example 4。

-------------------------------------------------------------- following is the source code of examples------------------------------------------------------

Example 1:
// FileInputDemo
// Demonstrates FileInputStream and DataInputStream
import java.io.*;

class FileInputDemo {
public static void main(String args[]) {
// args.length is equivalent to argc in C
if (args.length == 1) {
try {
// Open the file that is the first command line parameter
FileInputStream fstream = new FileInputStream(args[0]);
// Convert our input stream to a DataInputStream
DataInputStream in = new DataInputStream(fstream);
// Continue to read lines while there are still some left to read
while (in.available() !=0) {
// Print file line to screen
System.out.println (in.readLine());
}
in.close();
} catch (Exception e) {
System.err.println("File input error");
}
}
else
System.out.println("Invalid parameters");
}
}

Example 2:
// FileOutputDemo
// Demonstration of FileOutputStream and PrintStream classes
import java.io.*;

class FileOutputDemo
{
public static void main(String args[]) {
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object

try {
// connected to "myfile.txt"
out = new FileOutputStream("myfile.txt");
// Connect print stream to the output stream
p = new PrintStream( out );
p.println ("This is written to a file");
p.close();
} catch (Exception e) {
System.err.println ("Error writing to file");
}
}
}

Example 3:
// FileReadTest.java
// User FileReader in JDK1.1 to read a file
import java.io.*;

class FileReadTest {
public static void main (String[] args) {
FileReadTest t = new FileReadTest();
t.readMyFile();
}

void readMyFile() {
String record = null;
int recCount = 0;
try {
FileReader fr = new FileReader("mydata.txt");
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = br.readLine()) != null) {
recCount++;
System.out.println(recCount + ": " + record);
}
br.close();
fr.close();
} catch (IOException e) {
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
}

}

Example 4:
// FileWriteTest.java
// User FileWriter in JDK1.1 to writer a file
import java.io.*;

class FileWriteTest {
public static void main (String[] args) {
FileWriteTest t = new FileWriteTest();
t.WriteMyFile();
}

void WriteMyFile() {
try {
FileWriter fw = new FileWriter("mydata.txt");
PrintWriter out = new PrintWriter(fw);
out.print(“hi,this will be wirte into the file!”);
out.close();
fw.close();
} catch (IOException e) {
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
}

}




主站蜘蛛池模板: 日本乱码一卡二卡三卡永久 | 日韩经典一区 | 午夜福利国产一级毛片 | 五月激情丁香 | 天天做天天欢天天爽 | 日韩a视频在线观看 | 亚洲色欲色欲www在线观看 | 亚洲成人精品久久 | 五月婷婷在线视频 | 日韩大片 | 亚洲图片综合区另类图片 | 亚洲综合图区 | 中文字幕自拍 | 欧美午夜免费观看福利片 | 欧洲乱码专区一区二区三区四区 | 日本成人网址 | 亚欧乱色一区二区三区 | 视频日韩 | 欧洲一区麻豆文化传媒 | 天天艹日日干 | 偷窥自拍亚洲色图 | 日本视频黄 | 亚洲欧美在线免费观看 | 婷婷开心六月久久综合丁香 | 色综合久久久久久久 | 视频一区在线播放 | 日韩在线第三页 | 日韩中文字幕一区 | 天天躁夜夜躁狠狠躁2021 | 亚洲视频一区在线观看 | 亚洲视频一区在线观看 | 四虎精品在线视频 | 日日摸日日干 | 在线午夜视频 | 四虎在线播放免费永久视频 | 五月婷婷亚洲综合 | 啪啪免费视频 | 日韩亚射吧 | 中文字幕亚洲综合久久男男 | 天天干夜夜操 | 十八成人网 |