import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.Vector; class FileIO { public File CreateFile(String outputPath) throws IOException{ File f = new File(outputPath); if(! f.exists()){ f.createNewFile(); } return f; } public void CreateFolder(String outputPath){ File f = new File(outputPath); if(! f.exists()){f.mkdir();} } public void CreateFolders(String outputPath){ File f = new File(outputPath); if(! f.exists()){f.mkdirs();} } public void DeleteFile(String path){ File f = new File(path); if(f.exists()){f.delete();} } public void AppendFile(File f, String text) throws FileNotFoundException{ BufferedWriter out = null; if(f.exists()){ try { out = new BufferedWriter(new FileWriter(f.getPath(),true)); out.write(text); out.newLine(); }catch (Exception ex) {ex.printStackTrace();} finally { try { if (out != null) { out.flush(); out.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } } public void AppendFileUTF(File f, String text) throws FileNotFoundException{ OutputStreamWriter out = null; BufferedWriter bfw = null; if(f.exists()){ try { out = new OutputStreamWriter(new FileOutputStream(f.getPath(),true),"UTF-8"); bfw = new BufferedWriter(out); bfw.write(text); bfw.newLine(); }catch (Exception ex) {ex.printStackTrace();} finally { try { if (bfw != null) { bfw.flush(); bfw.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } } public void AppendFile(File f, String text, String encoding) throws FileNotFoundException{ OutputStreamWriter out = null; BufferedWriter bfw = null; if(f.exists()){ try { out = new OutputStreamWriter(new FileOutputStream(f.getPath(),true),encoding); bfw = new BufferedWriter(out); bfw.write(text); bfw.newLine(); }catch (Exception ex) {ex.printStackTrace();} finally { try { if (bfw != null) { bfw.flush(); bfw.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } } public BufferedReader readFile(File f, String encoding) throws FileNotFoundException, UnsupportedEncodingException{ BufferedReader br = null; if(f.exists()){ br = new BufferedReader(new InputStreamReader(new FileInputStream(f),encoding)); } return br; } public BufferedReader readFile(String path, String encoding) throws FileNotFoundException, UnsupportedEncodingException{ BufferedReader br = null; File f = new File(path); if(f.exists()){ br = new BufferedReader(new InputStreamReader(new FileInputStream(f),encoding)); } return br; } public File[] getFilesFromFolder(String folderPath){ File[] files = null; Vector<String> paths = new Vector<String>(); File f = new File(folderPath); if(f.exists()){ File[] fs = f.listFiles(); for(int i = 0; i<=fs.length-1; i++){ if(! fs[i].isDirectory()){ paths.add(fs[i].getPath()); } } files = new File[paths.size()]; for(int i = 0; i<=paths.size()-1 ; i++){ files[i] = new File(paths.get(i)); } } return files; } public File[] getFilesFromFolder(String folderPath, String fileFormat){ File[] files = null; fileFormat = fileFormat.toLowerCase(); Vector<String> paths = new Vector<String>(); File f = new File(folderPath); if(f.exists()){ File[] fs = f.listFiles(); for(int i = 0; i<=fs.length-1; i++){ if(! fs[i].isDirectory() && fs[i].getPath().toLowerCase().endsWith(fileFormat)){ paths.add(fs[i].getPath()); } } files = new File[paths.size()]; for(int i = 0; i<=paths.size()-1 ; i++){ files[i] = new File(paths.get(i)); } } return files; } public Vector<String> getAllFilesPathsFromFolder(String folderPath){ Vector<String> paths = new Vector<String>(); File f = new File(folderPath); if(f.exists()){ File[] fs = f.listFiles(); for(int i = 0; i<=fs.length-1; i++){ if(! fs[i].isDirectory()){ paths.add(fs[i].getPath()); }else if(fs[i].isDirectory()){ Vector<String> temp = getAllFilesPathsFromFolder(fs[i].getPath()); for(int j = 0; j<=temp.size()-1; j++){ paths.add(temp.get(j)); } } } } return paths; } public Vector<String> getAllFilesPathsFromFolder(String folderPath, String fileFormat){ fileFormat = fileFormat.toLowerCase(); Vector<String> paths = new Vector<String>(); File f = new File(folderPath); if(f.exists()){ File[] fs = f.listFiles(); for(int i = 0; i<=fs.length-1; i++){ if(! fs[i].isDirectory() && fs[i].getPath().toLowerCase().endsWith(fileFormat)){ paths.add(fs[i].getPath()); }else if(fs[i].isDirectory()){ Vector<String> temp = getAllFilesPathsFromFolder(fs[i].getPath(),fileFormat); for(int j = 0; j<=temp.size()-1; j++){ paths.add(temp.get(j)); } } } } return paths; } public File[] getAllFilesFromFolder(String folderPath){ Vector<String> paths = this.getAllFilesPathsFromFolder(folderPath); File[] files = null; if(paths.size()>0){ files = new File[paths.size()]; for(int i = 0; i<=files.length-1; i++){ files[i] = new File(paths.get(i)); } } return files; } public String getSystemRoot(){return System.getProperty("user.dir") + this.getFileSeparator();} public String getFileSeparator(){return System.getProperty("file.separator");} public String getLineSeparator(){return System.getProperty("line.separator");} public String getSystemEncode(){return System.getProperty("file.encoding");} }