java中讀寫文字檔案
寫文字資料
方法 一:
import java.io.*;
public class A {
public static void main(String args[]) {
FileOutputStream out;
PrintStream ps;
try {
out = new FileOutputStream("a.txt");
ps = new PrintStream(out);
ps.println("qun qun.");
ps.println("fei fei");
ps.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
方法 二:
import java.io.*;
public class B {
public static void main(String args[]) {
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter("b.txt");
pw = new PrintWriter(fw);
pw.print("qunqu n ");
pw.println("feiefi ss");
pw.print("qunqu n ");
pw.close();
fw.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
方法三:
import java.io.*;
public class C {
public static void main(String args[]) {
String str_written = "This is a simple example";
try {
FileWriter fwriter = new FileWriter("c.txt");
BufferedWriter bfwriter = new BufferedWriter(fwriter);
bfwriter.write(str_written, 0, str_written.length());
bfwriter.flush();
bfwriter.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
附註:方法一和方法二,方法三都是在操作文字檔案不存在的時候將建立,否則,當覆蓋之!
令;方法三
BufferedWriter將文字寫入字元輸出流,緩衝各個字元,從而提供單個字元、陣列和字串的高效寫入。
附:追加寫入:
import java.io.*;
public class C {
public static void main(String args[]) {
String str_written = "This is a simple example";
try {
FileWriter fwriter = new FileWriter("c.txt", true);
BufferedWriter bfwriter = new BufferedWriter(fwriter);
bfwriter.newLine();
bfwriter.write(str_written, 0, str_written.length());
bfwriter.flush();
bfwriter.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
讀文字資料
方法一:
import java.io.*;
public class A {
public static void main(String args[]) {
try {
FileInputStream fstream = new FileInputStream("a.txt");
DataInputStream in = new DataInputStream(fstream);
while (in.available() != 0) {
String a = in.readLine();
System.out.println(a);
System.out.println(a.length());
}
in.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
方法二:
import java.io.*;
public class B {
public static void main(String args[]) {
try {
FileReader fr = new FileReader("a.txt");
BufferedReader br = new BufferedReader(fr);
String str;
int count = 0;
while ((str = br.readLine()) != null) {
count++;
System.out.println(count + " : " + str);
}
br.close();
fr.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
附:方法二的能夠高效的實現文字資料的讀出
相關推薦
java中讀寫文字檔案
寫文字資料 方法 一: import java.io.*; public class A { public static void main(String args[]) { FileOutputStream out; PrintStr
Java使用RandomAccessFile讀寫文字檔案
指定位置寫入 RandomAccessFile file = new RandomAccessFile("c:\\k.txt", "rw"); file.seek(2*2);//跳過倆個位元組 file.write("人".getBytes());//防止亂碼 指定位
在Java中讀寫UTF-8編碼檔案
在C#中,採用System.IO.StreamReader和System.IO.StreamWriter直接讀寫UTF-8編碼的檔案,因為在這兩個類可以指定讀取或者寫入檔案的編碼格式,而其預設編碼則是UTF-8。 而在Java中,如果我們用java.io.FileReade
C#中讀寫INI檔案的方法例子
[DllImport(“kernel32”)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImp
java實現讀寫伺服器檔案
https://github.com/MAXIAODONGS/Remote-operation-of-static-resources 這個主要實現java遠端訪問伺服器的讀寫檔案操作,自動登入讀寫檔案,以上程式碼整理來自網際網路,然後自己將很多瑣碎的東西整理在了一起 pom.xml要配置
Java學習——讀寫txt檔案
package HHH; import java.io.*; import static java.lang.System.out; public class OpenFile { public static void main(String[] args) {
JAVA中 IO流文字檔案的讀取方式(一)(read 的用法)
JAVA中 IO流文字檔案的讀取方式(read 用法) 首先,我們先用通常的read方法去讀 程式碼 import java.io.FileReader; import java.io.IOException; public class FileReaderd
JAVA中IO流文字檔案的操作(BufferedWriter和BufferedReader 緩衝區)
JAVA 中 字元流的緩衝區 分為了BufferedWriter 和BufferedReader 先講BufferedWriter 作用: 緩衝區要結合流才可以使用,而且在流的基礎上對流的功能進行了增強。我們也可以說,在我們以後的實際運用之中,為了增強一下I
android 中讀寫xml檔案時取得路徑的方法/data/sdcard/src
package com.eboy.readwritexml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream;
Java POI 讀寫Excel 檔案簡單實現
整理FileUtils的一些方法,只是一些初步實現,剛寫完就掛上來了… 友情提示:**過於結構化,沒太多潤色....碼的不好還請諸位海涵並多提意見** 關聯的型別 資源 型別 說明 Workbook 介面 Ex
VBA建立文字檔案、讀寫文字檔案
Private Sub CommandButton1_Click() Dim gPath As String Dim sFile As Object, Fso As Object gPath = Application.ActiveWork
Flask成長筆記--如何在Flask框架裡面讀寫文字檔案
一、設定根目錄 我在工程專案中有一個專門的configure.py,用於寫全域性的配置。 我的文字檔案在工程目錄的static/txt中 #encoding: utf-8 import
C/C++讀寫文字檔案、二進位制檔案
//採用CPP模式讀取txt void TextRead_CPPmode() { fstream f; f.open("txt_out.txt",ios::in); //檔案開啟方式選項: // ios::in = 0x01, //供讀,檔案不存在則建立(ifstream預設的開啟方式) /
C# 讀寫文字檔案並匯入Excel(一)
一、本程式的主要思想 利用ReadAllText和WriteAllText方法讀寫文字檔案,並且把他們以逗號分隔,形成能被Excel直接匯入並且分列的資料。 二、程式的執行結果 執行前,必須在相應目錄建立文字檔案,方便程式的讀寫。程式執行前的資料如圖: 程
Java IO讀寫大檔案的幾種方式及測試
讀取檔案大小:1.45G 第一種,OldIO: Java程式碼 publicstaticvoid oldIOReadFile() throws IOException{ BufferedReader br = new BufferedReader(new File
C++中讀寫txt檔案並分離字元
在實際工程中,經常遇到需要讀取txt檔案,txt檔案中存的是一些小數或者整型資料,在C++中,可以利用string類和ifstream庫檔案對txt進行的讀取,不過讀回的資料經常是以字串的形式返回,一般是txt的一行為一個字串返回。那麼如何從字串中分離出整數或者是小數就涉及
在C++ Builder 和GCC(MinGW)中讀寫資料檔案的類
常常在GCC(MinGW)和C++ Builder 中都有讀寫資料的時候,也就是從檔案中將一組資料讀入二維陣列<vector>,或者將陣列中的資料格式化寫入檔案,甚至有時還想給檔案加個檔案頭,當然檔案頭也要對齊了才好看一點,兩個軟體實現的方法都不一樣,常常讓人惱
delphi讀寫文字檔案
procedureTForm1.Button1Click(Sender: TObject); //寫檔案var wText:TextFile;begin AssignFile(wText, 'ip.t
C#讀寫文字檔案
C#中讀寫文字檔案.txt檔案既可以用File類也可用StreamReader、StreamWrite類。這兩種方法都需要引用using System.IO名稱空間。下面分別給出例子:1.File類寫入
Qt中讀寫xml檔案
1 概述 Qt中解析XML檔案有多種方法,參考資料[1]使用QXmlStreamReader類來讀取並解析xml檔案,而參考資料[2]則使用QDomDocument類來解析xml檔案。 2 QDomDocument 參考資料[2][3][4]都給出了QDom