Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,726 members, 7,809,770 topics. Date: Friday, 26 April 2024 at 02:35 PM

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 (1043 Views)

Hello, An Aspiring Java Programmer Here! Like-minded Friends Oya Unite / Java Programmer Needs Help / 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

(1) (Reply)

Using Google Recaptcha With PHP - Are You A Robot? / First 5 People To Contact Me And Learn C# Programming Free In Egbeda / How to compress (reduce) the size of any file

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.