Posted: Sun Sep 19, 2004 3:16 pm Post subject: quick question writing to a file
Hi.
I am trying to write to a file a bunch of mailing information and stuff but the cursor always gets set back to the beginning...is there a way to maybe make the cursor go down a line and keep writing from there or somehting? Here's the code, thanks a bunch:
code:
import java.io.*;
public class Mailing1RA
{
public static void main(String args[]) throws IOException
{
String[] info = new String[5];
String empty = "";
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(converter);
for (int i = 0;i<5;i++)
{
try
{
FileWriter fOut = new FileWriter("name.txt");
PrintWriter fileOut = new PrintWriter(fOut, true);
switch(i)
{
case 0://full name
System.out.println("Please enter your full name");
info[i] = console.readLine();
while(!info[0].equals(empty))
{
fileOut.println(info[0]);
info[0] = console.readLine();
}//end while
break;
case 1://address
System.out.println("Please enter your address");
info[i] = console.readLine();
while(!info[1].equals(empty))
{
fileOut.println(info[1]);
info[1] = console.readLine();
}//end while
break;
case 2://city
System.out.println("Please enter your city");
info[i] = console.readLine();
while(!info[2].equals(empty))
{
fileOut.println(info[2]);
info[2] = console.readLine();
}//end while
break;
case 3://province/state/territory
System.out.println("Please enter your province/state/territory");
info[i] = console.readLine();
while(!info[3].equals(empty))
{
fileOut.println(info[3]);
info[3] = console.readLine();
}//end while
break;
case 4://postal code
System.out.println("Please enter your postal code");
info[i] = console.readLine();
while(!info[4].equals(empty))
{
fileOut.println(info[4]);
info[4] = console.readLine();
}//end while
break;
Posted: Sun Sep 19, 2004 3:45 pm Post subject: (No subject)
its because the..
code:
FileWriter fOut = new FileWriter("name.txt");
PrintWriter fileOut = new PrintWriter(fOut, true);
lines are within your for-loop.. you need to move them outside the loop.
SsJBebiGoku
Posted: Sun Sep 19, 2004 4:08 pm Post subject: (No subject)
thanks a lot...one more little question - do you know if there is a way to rename a file? in this case, i need to rename it to the user's name...
thanks
rizzix
Posted: Sun Sep 19, 2004 7:26 pm Post subject: (No subject)
yes there is..
for example:
code:
File myCurrenFile = new File("oldfile");
File myRenamedFile = new File("newfile");
if (myCurrentFile.renameTo(myRenamedFile)) {
// successful rename
}