運行效果:
代碼:
package com.io; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.Scanner; import java.util.Vector; public class CreateAndReadTxt { // 文件名稱 public static String fileName = ".txt"; public static String newFileName = ".txt"; // 文件路徑 public final static String URL = System.getProperty("user.dir"); // CreateAndReadTxt.class.getResource("/").getPath(); // 建立TXT文件 public static void createTxtFile(String fName, String fileContent) { // 建立文件 fileName = fName + fileName; File file = new File(fileName); // 能夠更改 file.setWritable(true); // 判斷當前路徑下是否存在同名文件 boolean isExist = file.exists(); if (isExist) { // 文件存在,刪除 file.delete(); } // 寫入文件 try { // 文件寫入對象 FileOutputStream fos = new FileOutputStream(file); // 輸入流寫入----默認字符爲GBK OutputStreamWriter osw = new OutputStreamWriter(fos); // 寫入 osw.write(fileContent); // 寫入完畢後關閉 osw.close(); System.out.println("成功建立文件:\t"+fileName); } catch (IOException e) { System.out.println("寫入文件失敗:\t" + e.getMessage()); } } // 閱讀文件 public static void readFile(String fileName) { System.out.println("開始讀取文件:\t" + fileName); // 產生文件對象 File file = new File(fileName); // try { // 字符讀取 FileReader fr = new FileReader(file); // 緩衝處理 BufferedReader br = new BufferedReader(fr); String str = ""; while ((str = br.readLine()) != null) { System.out.println(str); } // 關閉 br.close(); fr.close(); } catch (FileNotFoundException e) { System.out.println("讀取文件失敗:\t" + e.getMessage()); } catch (IOException e) { System.out.println("讀取文件失敗:\t" + e.getMessage()); } } // 文件複製 public static void copyFile(String fromFileName,String toFileName){ //讀取文件 File file = new File(fromFileName); try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); // 定義接收變量 Vector<Double> vec = new Vector<Double>(); String s = ""; while(null!=(s=br.readLine())){ vec.add(Double.parseDouble(s)); } br.close(); fr.close(); // 保存到數組並進行排序 Double dou[] = new Double[vec.size()]; vec.toArray(dou); Arrays.sort(dou); System.out.println("========複製文件========="); // 寫入新文件 newFileName = "副本"+newFileName; File newFile = new File(toFileName); FileOutputStream fos = new FileOutputStream(newFile, true); OutputStreamWriter osm = new OutputStreamWriter(fos); for(Double d:dou){ osm.write(d.doubleValue()+"\n"); } osm.close(); fos.close(); } catch (FileNotFoundException e) { System.out.println("讀取文件失敗:\t" + e.getMessage()); } catch (IOException e) { System.out.println("讀取文件失敗:\t" + e.getMessage()); } } public static void main(String[] args) { /** * 構造數據 */ Scanner scan = new Scanner(System.in); StringBuilder sb = new StringBuilder(); String s = ""; while(!("end".equals(s = scan.next()))){// 當輸入end時,結束 sb.append(s); sb.append("\n"); } scan.close(); /** * 使用數據 */ CreateAndReadTxt.createTxtFile("creat", sb.toString()); CreateAndReadTxt.readFile(fileName); System.out.println(fileName); CreateAndReadTxt.copyFile(fileName, newFileName); CreateAndReadTxt.readFile(newFileName); } }