Saturday, October 10, 2015

Java read from file and write to file

   /* File fin = new File(<Absolute path of the file>); */  
   public void fileRead(File fin) {  
     try {  
       FileInputStream fis = new FileInputStream(fin);  
       BufferedReader br = new BufferedReader(new InputStreamReader(fis));  
       String line = null;  
       while((line=br.readLine()) != null) {  
         System.out.println(line);  
       }  
       br.close();  
     } catch (IOException e) {  
       System.err.println(e.getMessage());  
     }  
   }  
   public void fileWrite(File fout, String line) {  
     try {  
       System.out.println(line);  
       FileOutputStream fos = new FileOutputStream(fout);  
       BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));  
       bw.append(line);  
       fos.close();  
     } catch (IOException e) {  
       System.err.println(e.getMessage());  
     }  
   }  

No comments:

Post a Comment