項目中要求批量txt文件能夠轉換爲java文件或xml文件,如下主要是總結的幾種IO讀寫的方法;java
一、按行讀寫txt文件和java文件,以treemap默認升序的有序map類型保存讀寫的結果轉換;web
1 package com.yss.util; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStreamReader; 11 import java.io.OutputStreamWriter; 12 import java.util.Map; 13 import java.util.TreeMap; 14 15 public class Test01 { 16 17 public static void main(String[] args) { 18 try { 19 readfile("C:\\知識\\yss"); 20 } catch (FileNotFoundException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } catch (IOException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 28 } 29 30 /** 31 * 刪除單個文件 32 * 33 * @param fileName 34 * 要刪除的文件的文件名 35 * @return 單個文件刪除成功返回true,不然返回false 36 */ 37 public static boolean deleteFile(String fileName) { 38 File file = new File(fileName); 39 // 若是文件路徑所對應的文件存在,而且是一個文件,則直接刪除 40 if (file.exists() && file.isFile()) { 41 if (file.delete()) { 42 System.out.println("刪除單個文件" + fileName + "成功!"); 43 return true; 44 } else { 45 System.out.println("刪除單個文件" + fileName + "失敗!"); 46 return false; 47 } 48 } else { 49 System.out.println("刪除單個文件失敗:" + fileName + "不存在!"); 50 return false; 51 } 52 } 53 54 private static void duxie(String name01,String name02) { 55 Map<Integer, String> map = new TreeMap<Integer, String>(); 56 57 int i=0; 58 /* 讀取數據 */ 59 try { 60 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(name01)), 61 "UTF-8")); 62 String lineTxt = null; 63 64 while ((lineTxt = br.readLine()) != null) { 65 map.put(i, lineTxt); 66 i++; 67 } 68 br.close(); 69 } catch (Exception e) { 70 System.err.println("read errors :" + e); 71 } 72 //建立一個文件 73 File fileName=new File(name02); 74 try{ 75 if(!fileName.exists()){ 76 fileName.createNewFile(); 77 } 78 }catch(Exception e){ 79 e.printStackTrace(); 80 } 81 //刪除掉原來的文件 82 deleteFile(name01); 83 84 /* 輸出數據 */ 85 try { 86 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(name02)), 87 "UTF-8")); 88 89 for (Integer ii : map.keySet()) { 90 bw.write(map.get(ii)); 91 bw.newLine(); 92 } 93 bw.close(); 94 } catch (Exception e) { 95 System.err.println("write errors :" + e); 96 } 97 98 } 99 100 /** 101 * 讀取某個文件夾下的全部文件 102 */ 103 public static boolean readfile(String filepath) throws FileNotFoundException, IOException { 104 try { 105 File file = new File(filepath); 106 if (!file.isDirectory()) { 107 System.out.println("文件"); 108 String path=file.getPath(); 109 System.out.println("name=" + file.getName()); 110 if(file.getName().indexOf("java")>0){ 111 String path01=file.getPath().replace(file.getName(), file.getName().replace("java", "txt")); 112 System.out.println(file.getName().replace("java", "txt")+"複製之後文件路徑:"+path01); 113 duxie(path,path01); 114 } 115 116 } else if (file.isDirectory()) { 117 System.out.println("文件夾"); 118 String[] filelist = file.list(); 119 for (int i = 0; i < filelist.length; i++) { 120 File readfile = new File(filepath + "\\" + filelist[i]); 121 if (!readfile.isDirectory()) { 122 123 String path=readfile.getPath(); 124 System.out.println("name=" + readfile.getName()); 125 if(readfile.getName().indexOf("java")>0){ 126 String path01=readfile.getPath().replace(readfile.getName(), readfile.getName().replace("java", "txt")); 127 System.out.println(readfile.getName().replace("java", "txt")+"複製之後文件路徑:"+path01); 128 duxie(path,path01); 129 } 130 } else if (readfile.isDirectory()) { 131 readfile(filepath + "\\" + filelist[i]); 132 } 133 } 134 135 } 136 137 } catch (FileNotFoundException e) { 138 System.out.println("readfile() Exception:" + e.getMessage()); 139 } 140 return true; 141 } 142 143 }
二、關於普通的txt文件讀寫,建立,刪除的簡單方法;函數
1 package com.yss.util; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.io.InputStreamReader; 10 11 public class Test { 12 /** 13 * 建立文件 14 * @param fileName 15 * @return 16 */ 17 public static boolean createFile(File fileName)throws Exception{ 18 try{ 19 if(!fileName.exists()){ 20 fileName.createNewFile(); 21 } 22 }catch(Exception e){ 23 e.printStackTrace(); 24 } 25 return true; 26 } 27 28 29 /** 30 *讀取TXT內容 31 * @param file 32 * @return 33 */ 34 public static String readTxtFile(File file){ 35 String result = ""; 36 try { 37 InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"gbk"); 38 BufferedReader br = new BufferedReader(reader); 39 String s = null; 40 while((s=br.readLine())!=null){ 41 result = result + s; 42 System.out.println(s); 43 } 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } 47 return result; 48 } 49 50 51 /** 52 * 寫入TXT,覆蓋原內容 53 * @param content 54 * @param fileName 55 * @return 56 * @throws Exception 57 */ 58 public static boolean writeTxtFile(String content,File fileName)throws Exception{ 59 boolean flag=false; 60 FileOutputStream fileOutputStream=null; 61 try { 62 fileOutputStream = new FileOutputStream(fileName); 63 fileOutputStream.write(content.getBytes("gbk")); 64 fileOutputStream.close(); 65 flag=true; 66 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 return flag; 70 } 71 72 73 /** 74 * 寫入TXT,追加寫入 75 * @param filePath 76 * @param content 77 */ 78 public static void fileChaseFW(String filePath, String content) { 79 try { 80 //構造函數中的第二個參數true表示以追加形式寫文件 81 FileWriter fw = new FileWriter(filePath,true); 82 fw.write(content); 83 fw.close(); 84 } catch (IOException e) { 85 System.out.println("文件寫入失敗!" + e); 86 } 87 } 88 89 90 91 public static void main(String[] args) throws Exception{ 92 File file = new File("C:\\知識\\123.txt"); 93 File file01 = new File("C:\\知識\\1234.txt"); 94 createFile(file01); 95 //readTxtFile(file); 96 writeTxtFile(readTxtFile(file)+"我是寫入的內容11",file01); 97 //fileChaseFW("C:\\知識\\1234.txt","66666666"); 98 } 99 100 }
三、刪除文件或文件夾,或此目錄下的文件的java實現方法;spa
1 package com.yss.util; 2 3 import java.io.File; 4 5 /** 6 * 刪除文件和目錄 7 * 8 */ 9 public class Test03 { 10 11 /** 12 * 刪除文件,能夠是文件或文件夾 13 * 14 * @param fileName 15 * 要刪除的文件名 16 * @return 刪除成功返回true,不然返回false 17 */ 18 public static boolean delete(String fileName) { 19 File file = new File(fileName); 20 if (!file.exists()) { 21 System.out.println("刪除文件失敗:" + fileName + "不存在!"); 22 return false; 23 } else { 24 if (file.isFile()) 25 return deleteFile(fileName); 26 else 27 return deleteDirectory(fileName); 28 } 29 } 30 31 /** 32 * 刪除單個文件 33 * 34 * @param fileName 35 * 要刪除的文件的文件名 36 * @return 單個文件刪除成功返回true,不然返回false 37 */ 38 public static boolean deleteFile(String fileName) { 39 File file = new File(fileName); 40 // 若是文件路徑所對應的文件存在,而且是一個文件,則直接刪除 41 if (file.exists() && file.isFile()) { 42 if (file.delete()) { 43 System.out.println("刪除單個文件" + fileName + "成功!"); 44 return true; 45 } else { 46 System.out.println("刪除單個文件" + fileName + "失敗!"); 47 return false; 48 } 49 } else { 50 System.out.println("刪除單個文件失敗:" + fileName + "不存在!"); 51 return false; 52 } 53 } 54 55 /** 56 * 刪除目錄及目錄下的文件 57 * 58 * @param dir 59 * 要刪除的目錄的文件路徑 60 * @return 目錄刪除成功返回true,不然返回false 61 */ 62 public static boolean deleteDirectory(String dir) { 63 // 若是dir不以文件分隔符結尾,自動添加文件分隔符 64 if (!dir.endsWith(File.separator)) 65 dir = dir + File.separator; 66 File dirFile = new File(dir); 67 // 若是dir對應的文件不存在,或者不是一個目錄,則退出 68 if ((!dirFile.exists()) || (!dirFile.isDirectory())) { 69 System.out.println("刪除目錄失敗:" + dir + "不存在!"); 70 return false; 71 } 72 boolean flag = true; 73 // 刪除文件夾中的全部文件包括子目錄 74 File[] files = dirFile.listFiles(); 75 for (int i = 0; i < files.length; i++) { 76 // 刪除子文件 77 if (files[i].isFile()) { 78 flag = Test03.deleteFile(files[i].getAbsolutePath()); 79 if (!flag) 80 break; 81 } 82 // 刪除子目錄 83 else if (files[i].isDirectory()) { 84 flag = Test03.deleteDirectory(files[i] 85 .getAbsolutePath()); 86 if (!flag) 87 break; 88 } 89 } 90 if (!flag) { 91 System.out.println("刪除目錄失敗!"); 92 return false; 93 } 94 // 刪除當前目錄 95 if (dirFile.delete()) { 96 System.out.println("刪除目錄" + dir + "成功!"); 97 return true; 98 } else { 99 return false; 100 } 101 } 102 103 public static void main(String[] args) { 104 String dir = "D:/home/web/upload/upload/files"; 105 Test03.deleteDirectory(dir); 106 107 } 108 109 }
四、打印某目錄下的文件名稱和此文件的路徑;code
1 package com.yss.util; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 7 public class Test02 { 8 /** 9 * 讀取某個文件夾下的全部文件 10 */ 11 public static boolean readfile(String filepath) throws FileNotFoundException, IOException { 12 try { 13 File file = new File(filepath); 14 if (!file.isDirectory()) { 15 System.out.println("文件"); 16 System.out.println("path=" + file.getPath()); 17 System.out.println("name=" + file.getName()); 18 19 } else if (file.isDirectory()) { 20 System.out.println("文件夾"); 21 String[] filelist = file.list(); 22 for (int i = 0; i < filelist.length; i++) { 23 File readfile = new File(filepath + "\\" + filelist[i]); 24 if (!readfile.isDirectory()) { 25 System.out.println("path=" + readfile.getPath()); 26 System.out.println("absolutepath=" 27 + readfile.getAbsolutePath()); 28 System.out.println("name=" + readfile.getName()); 29 30 } else if (readfile.isDirectory()) { 31 readfile(filepath + "\\" + filelist[i]); 32 } 33 } 34 35 } 36 37 } catch (FileNotFoundException e) { 38 System.out.println("readfile() Exception:" + e.getMessage()); 39 } 40 return true; 41 } 42 43 public static void main(String[] args) { 44 String l="Activator.java"; 45 System.out.println(l.replace("java", "txt")+">>>>>>>>>>>"); 46 } 47 48 }
以上的方法類都是相關的txt文件或java類型的文件的讀寫。xml