write text to a file help
i dont have much knowledge on streams output streams mostly , and right now i need to make a method that will write text to a file
here's all that i have set up >.<
info[0] = the file name
info[1] = the stuff to write to that file
Code :
public void fileWriter(String[] info){
}
yes , i know its bare XP but thats about all i know about the output streams :((
Re: write text to a file help
If you look at the bottom of this page there is a list of similar threads.
Have a look at http://www.javaprogrammingforums.com...sing-java.html
// Json
Re: write text to a file help
oh wow , i never noticed that b4 XP *fails*
thanks for the help though ^_^
Re: write text to a file help
Hello to all,
I have managed to write to a file but is keeps writing on the same line.
I’ve tried «\n" but the .txt file recognizes it as a character.
any ideas?
thank you
Re: write text to a file help
Print the following to the file stream.
Code :
System.getProperty("line.separator");
Chris
Re: write text to a file help
Code :
public static boolean saveStringToFile(String fileName, String saveString)
{
boolean saved = false;
BufferedWriter bw = null;
try
{
// bw = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
bw= new BufferedWriter(new FileWriter(fileName));
try
{
//bw.println();
bw.write(saveString);
System.getProperty("line.separator");
//bw.newLine();
saved = true;
}//end of try
finally
{
bw.close();
}//end of finally
}//end of try
catch (IOException ex)
{
ex.printStackTrace();
}//end of catch
return saved;
}//end of saveStringToFile method
this is my code.
i tried the line.separator as well as .newlive() and both didn't work
Re: write text to a file help
If using the BufferedWriter class then the .newLine() function would work. However, i'm not to sure where you are specifying that there should be a new line printed. From my understanding you are passing a String of text (one line) to a function which then write that line to the file and then prints a new line.
Could you please give an example of inputs to the function and the desired output.
Regards,
Chris
Re: write text to a file help
Well am using a queue simulation and I want to write every line of the queue
The code below shows more details
Code :
if(tillQueue.peek() == null)
System.out.println(i+"\t"+"\t"+ getCurrentQLength()+"\t"+"Queue Empty"+"\t"+"Info Saved: "+saveStringToFile("Queue_Info.txt", tillQueue.toString()));
else
System.out.println(i+"\t"+ "\t"+ getCurrentQLength()+ "\t"+tillQueue.toString()+"\t"+"Info Saved: "+saveStringToFile("Queue_Info.txt", tillQueue.toString()));
Re: write text to a file help
Try opening the file in append mode, new FileWriter(fileName, true); Also you will need to loop trhough each line of the Queue rather than just dumping it onto the saveStringToFile() function. But yes open in amend, print a line, print a blank line (bw.newLine()), close repeat.
Regards,
Chris