IO read write file file

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test {
    
    public static void main(String[] args) {
        readFile();
    }
    
    /**
     * 字符串转换
     * @param from
     * @return 
     * / 
    Private  static String translateStr (String from) {
         return "TRUNCATE Table` thc_rcm`. "+ from +" `;" ; 
    } 
    
     / ** 
     * reads TXT file 
     * / 
    public  static List <String> readFile () { 
        List <String> List = new new the ArrayList <> ();
         int COUNT = 0 ; 
        the Set <String> SET = new new HashSet <> (); 
        String pathname = "/ the Users / guchunchao / Workspace-SST / the Test / the src / D. TXT "; // absolute or relative path can demonstrate the relative path when writing files, reading input more paths.
        txt file// prevent file creation or read failed, with a catch catch the error and print, can the throw;
         // without closing the file lead to the disclosure, document literacy resources have empathy
         // Java7 the try-with-resources gracefully shut down file, closing the file automatically when abnormality; detailed interpretation HTTPS: // stackoverflow.com/a/12665271 
        the try (the FileReader Reader = new new the FileReader (pathname); 
             the BufferedReader br = new new the BufferedReader (Reader) // create an object, that the contents of the file turn into a computer can read the language 
        ) { 
            String Line; 
            // recommended by friends more concise wording 
            the while ! ((Line = br.readLine ()) = null ! && . line.trim () isEmpty ()) { 
                
                // read into a row of data
 //                System.out.println(line);
//                String[] codeValue = line.split(",");
//                String sql = "insert into `thc_passport`.`city` values('" + codeValue[0] + "','"+ codeValue[1] + "');";
                System.out.println(++count + " " +translateStr(line));
                
//                set.add(sql);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            
        }
        list.addAll(set);
        return list;
    
    staticpublic* /
     * write a TXT file/ **
    }
     
      void the writeFile () { 
        BufferedWriter, OUT = null ;
         the try { 
            File writeName = new new File ( "/ the Users / guchunchao / Workspace-SST / the Test / the src / e.txt"); // relative path, if not will have to establish a new the output.txt file 
            writeName.createNewFile (); // create a new file with the same name as the file directly, then covered 
            FileWriter Writer = new new FileWriter (writeName); 
            OUT = new new BufferedWriter (Writer); 
          
                List <String> List = readFile () ;
                 for ( int I = 0; I <list.size (); I ++) { 
                    Out.write (List.get (I) + "\ R & lt \ n-"); // \ R & lt \ is the n-newline 
                } 
                out.flush (); // the file buffer contents pressed 
        } the catch (IOException E) { 
            e.printStackTrace (); 
            the try { 
                the out.close (); 
            } the catch (IOException EX) { 
                ex.printStackTrace (); 
            } 
        } the finally {
             the try { 
                the out.close (); 
            } the catch (IOException E) {
                e.printStackTrace (); 
            } 
        } 
    } 


}

 

Guess you like

Origin www.cnblogs.com/guchunchao/p/11542193.html