I Am Stuck In My Project...i Need Help From A Good Java Programmer - Programming - Nairaland
Nairaland Forum / Science/Technology / Programming / I Am Stuck In My Project...i Need Help From A Good Java Programmer (1072 Views)
A Website With All Algorithm Solutions (my Project) / Hello, An Aspiring Java Programmer Here! Like-minded Friends Oya Unite / Java Programmer Wanted (2) (3) (4)
(1) (Reply)
I Am Stuck In My Project...i Need Help From A Good Java Programmer by NigerianScholar: 10:49am On Oct 31, 2017 |
without boring you with the details..ill just say what i need to be done in my code. btw, i have imported the java io package 1. i need to be able to save the user input in a new file e.g txt file 2. i need to be able to save the user input in an existing file i.e to update 3. to list all the file names/directories in the folder.. to do it in cmd prompt you nav to the path and type dir 4. i need to be able to view a file specified by the user
Thank you for your help...feel free to mention anyone that may have relevant knowledge in this or give me the link to a java group chat |
Re: I Am Stuck In My Project...i Need Help From A Good Java Programmer by seunthomas: 3:09pm On Oct 31, 2017 |
import java.io.*; import java.util.*;
class Help{ private File file;
protected File createFile(String filename){ try{ file = new File(filename); if (!file.exists()) { file.createNewFile(); }
} catch(Exception ex){ ex.printStackTrace(); } return file; } public String readFile(String filename){ String output=""; try{ file = new File(filename); FileReader fr = new FileReader(file.getAbsolutePath()); BufferedReader br = new BufferedReader(fr);
String data;
while ((data = br.readLine()) != null) { output = output + data ; } } catch(Exception ex){ ex.printStackTrace(); } return output; }
public void appendToFile(String filename,String data){
try { boolean append=true; writeToFile(filename, data,append); } catch(Exception ex) { ex.printStackTrace(); }
}
//String filename //String data to write //boolean append or just write public void writeToFile(String filename,String data,boolean append){ try { File f = createFile(filename); FileWriter fwrite = new FileWriter(f.getAbsoluteFile(),append); fwrite.write(data); fwrite.close(); } catch(Exception ex) { ex.printStackTrace(); }
}
public File[] getDir(String dir){ File folder = new File(dir); File[] files = folder.listFiles(); return files; }
public static void main(String args[]){ String filename="test.txt"; Help hlp = new Help(); hlp.writeToFile(filename,"Hello World\n",false); for(int i=0;i<=1000;i++){ hlp.appendToFile(filename,"Hello World "+String.valueOf(i)+"\n" ) ; } }
}
2 Likes |
Re: I Am Stuck In My Project...i Need Help From A Good Java Programmer by NigerianScholar: 3:41am On Nov 01, 2017 |
seunthomas:
import java.io.*; import java.util.*;
class Help{ private File file;
protected File createFile(String filename){ try{ file = new File(filename); if (!file.exists()) { file.createNewFile(); }
} catch(Exception ex){ ex.printStackTrace(); } return file; } public String readFile(String filename){ String output=""; try{ file = new File(filename); FileReader fr = new FileReader(file.getAbsolutePath()); BufferedReader br = new BufferedReader(fr);
String data;
while ((data = br.readLine()) != null) { output = output + data ; } } catch(Exception ex){ ex.printStackTrace(); } return output; }
public void appendToFile(String filename,String data){
try { boolean append=true; writeToFile(filename, data,append); } catch(Exception ex) { ex.printStackTrace(); }
}
//String filename //String data to write //boolean append or just write public void writeToFile(String filename,String data,boolean append){ try { File f = createFile(filename); FileWriter fwrite = new FileWriter(f.getAbsoluteFile(),append); fwrite.write(data); fwrite.close(); } catch(Exception ex) { ex.printStackTrace(); }
}
public File[] getDir(String dir){ File folder = new File(dir); File[] files = folder.listFiles(); return files; }
public static void main(String args[]){ String filename="test.txt"; Help hlp = new Help(); hlp.writeToFile(filename,"Hello World\n",false); for(int i=0;i<=1000;i++){ hlp.appendToFile(filename,"Hello World "+String.valueOf(i)+"\n" ) ; } }
}
thank you i should get back to you as soon as i try it |