Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Process text files from one directory to another  (Read 602 times)
0 Members and 1 Guest are viewing this topic.
G. Ritardo
Member
*
*

Reputation: 0
Offline Offline
Posts: 53
Referrals: 0

Awards
« on: May 13, 2009, 04:32:38 PM »

Code
GeSHi (java):
  1. import java.io.File;
  2. import java.io.*;
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.InputStreamReader;
  6.  
  7. public class ListFiles
  8. {
  9. public static String path1;
  10. public static String path2;
  11. public static String files;
  12. public static String fileName;
  13.  
  14. public static void main(String[] args) throws Exception
  15. {
  16.   path1 = "/dir1/";
  17.   path2 = "/dir2/";
  18.  
  19.  File folder = new File(path1);
  20.  File[] listOfFiles = folder.listFiles();
  21.  for (int i = 0; i < listOfFiles.length; i++)
  22.  {
  23.  
  24.   if (listOfFiles[i].isFile())
  25.   {
  26.   files = listOfFiles[i].getName();
  27.       if (files.endsWith(".txt") || files.endsWith(".TXT"))
  28.       {
  29.  
  30.          System.out.println(files);
  31.  
  32.          try
  33.          {
  34.          FileInputStream in = new FileInputStream(path1 + files);
  35.          BufferedReader br = new BufferedReader(new InputStreamReader(in));
  36.          String strLine;
  37.  
  38.     Writer output;
  39.     File file1 = new File(path2 + files);
  40.     String newline = System.getProperty("line.separator");
  41.     output = new BufferedWriter(new FileWriter(file1));
  42.  
  43.          while((strLine = br.readLine())!= null)
  44.          {
  45.           //Write contents of text file
  46.           output.write(strLine);
  47.           output.write(newline);
  48.          //System.out.println(strLine);
  49.          }
  50.  
  51.           output.close();
  52.  
  53.          }catch(Exception e){
  54.           System.out.println(e);
  55.          }
  56.  
  57.        }
  58.       }
  59.  }
  60. }
  61. }
Created by GeSHI 1.0.7.20
Logged

Founder of www.Retardation-Foundation.com :: A community for retarded people!? Powered by admin Gaylord Ritardo.
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here