import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class myIO
{
public static void main(String[] args) throws Exception
{
ReadFile("C:\\abc.txt"); // abc是你在C槽中的一個txt檔
}
public static void ReadFile(String path)
{
try
{
String str = new String();
BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf8")); // 設定為utf8格式
while (read.ready())
{
String brStr = read.readLine();
str = str + brStr;
}// End While
System.out.println("這個檔案的字串:" + str);
WriteFile(str, "C:\\123.txt", false); //寫到你C槽中的123.txt檔
} catch (Exception e)
{
e.printStackTrace();
System.out.println(path + "讀檔錯誤!!");
}
}
public static void WriteFile(String str, String path, boolean append)
{ // 寫檔
try
{
File file = new File(path);// 建立檔案,準備寫檔
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), "utf8"));// 設定為BIG5格式
// 參數append代表要不要繼續許入該檔案中
writer.write(str); // 寫入該字串
writer.newLine(); // 寫入換行
writer.close();
} catch (IOException e)
{
e.printStackTrace();
System.out.println(path + "寫檔錯誤!!");return false;
}
}
}
讀檔時要確定你的檔案路徑是否存在喔!!~
by ting