Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Text File Saving
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Fiend-Master




PostPosted: Fri Jan 21, 2005 9:50 pm   Post subject: Java Text File Saving

ok i need some help with saving info to a text file without using a console. this is my code, and i need to know how to get my program to run (and save the file with the selected text) without having to input information uselessly. it is for an rpg i am making, and i need to be able to do this. any help would be appreciated. here is the code:

Java:
import java.io.*;

public class Testing
{
    public static void main (String args[]) throws IOException
    {
        String[] info = new String [5];
        String empty = "";
        int temp = 0;

        InputStreamReader converter = new InputStreamReader (System.in);
        BufferedReader console = new BufferedReader (converter);

        FileWriter FileName = new FileWriter ("test.txt");
        PrintWriter NewPrintWriter = new PrintWriter (FileName, true);

        try
        {
            NewPrintWriter.println ("Character Name");
            NewPrintWriter.println ("Character Level");
            NewPrintWriter.println ("Character Attack");
            NewPrintWriter.println ("Character Potions");
            NewPrintWriter.println ("Everything Else");
            info [0] = console.readLine (); /*need to find out how to save  without this line*/
           
            NewPrintWriter.close ();
        } //end try

        catch (IOException e)
        {
            System.out.println ("Error: " + e);
        }

    } //end main
} //end Testing
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Jan 21, 2005 10:18 pm   Post subject: (No subject)

First off, import the java.lang package so you can use the String class.

Java:
import java.lang.*;


Next, you can get rid of the extra variables.

Java:
import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main (String args[]) throws IOException
    {
        String[] info = new String[5];
        String empty = "";
        int temp = 0;

        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter NewPrintWriter = new PrintWriter(new FileWriter("test.txt"), true);

        try
        {
            NewPrintWriter.println ("Character Name");
            NewPrintWriter.println ("Character Level");
            NewPrintWriter.println ("Character Attack");
            NewPrintWriter.println ("Character Potions");
            NewPrintWriter.println ("Everything Else");
            info [0] = console.readLine (); /*need to find out how to save  without this line*/
           
            NewPrintWriter.close ();
        } //end try

        catch (IOException e)
        {
            System.out.println ("Error: " + e);
        }

    } //end main
} //end Testing


Next, variable names should begin with lowercase letters, and there's no need for spaces between function names and parentheses.

Java:
import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main(String args[]) throws IOException
    {
        String[] info = new String[5];
        String empty = "";
        int temp = 0;

        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter newPrintWriter = new PrintWriter(new FileWriter("test.txt"), true);

        try
        {
            newPrintWriter.println("Character Name");
            newPrintWriter.println("Character Level");
            newPrintWriter.println("Character Attack");
            newPrintWriter.println("Character Potions");
            newPrintWriter.println("Everything Else");
            info[0] = console.readLine(); /*need to find out how to save  without this line*/
           
            newPrintWriter.close();
        } //end try

        catch (IOException e)
        {
            System.out.println("Error: " + e);
        }

    } //end main
} //end Testing


Oh, and "String args[]" should be "String[] args".

Next, the close of the print writer should be in the "finally" clause of the try...catch. Oh, and we can get rid of the useless comments. Smile

Java:
import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main(String[] args) throws IOException
    {
        String[] info = new String[5];
        String empty = "";
        int temp = 0;

        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter newPrintWriter = new PrintWriter(new FileWriter("test.txt"), true);

        try
        {
            newPrintWriter.println("Character Name");
            newPrintWriter.println("Character Level");
            newPrintWriter.println("Character Attack");
            newPrintWriter.println("Character Potions");
            newPrintWriter.println("Everything Else");
            info[0] = console.readLine(); /*need to find out how to save  without this line*/
        }
        catch (IOException e)
        {
            System.out.println("Error: " + e);
        }
        finally
        {
            newPrintWriter.close();
        }
    }
}


The following doesn't serve any purpose at all, except to make the program pause.

Java:
info[0] = console.readLine(); /*need to find out how to save  without this line*/


Oh, and if we have an error message, we should be printing it to the error handle. Smile

And also a bunch of variables here are useless, so we can eliminate them.

Java:
import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main(String[] args) throws IOException
    {
        PrintWriter newPrintWriter = new PrintWriter(new FileWriter("test.txt"), true);

        try
        {
            newPrintWriter.println("Character Name");
            newPrintWriter.println("Character Level");
            newPrintWriter.println("Character Attack");
            newPrintWriter.println("Character Potions");
            newPrintWriter.println("Everything Else");
        }
        catch (IOException e)
        {
            System.err.println("Error: " + e);
        }
        finally
        {
            newPrintWriter.close();
        }
    }
}


Now, you have the actual part of your program that's actually doing anything, and it saves the text to "test.txt" without any trouble.
Fiend-Master




PostPosted: Sat Jan 22, 2005 10:09 am   Post subject: (No subject)

whoa i didnt realize my coding could be shortened by so much, I guess thats why my name is "Fiend-Master" instead of "Programming-Master" lol. but yea i tried ur version and i got this error: "This catch block is unreachable because there is no exception whose type is assignable to "java.io.IOException" that can be thrown during execution of the body of the try block." and thats the same error I got, which is what i wanted to know how to get around. I dont know how to get it to work without that line.
Hikaru79




PostPosted: Sat Jan 22, 2005 12:30 pm   Post subject: (No subject)

Java:
import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main(String[] args) throws IOException
    {
        PrintWriter newPrintWriter = new PrintWriter(new FileWriter("test.txt"), true);

            newPrintWriter.println("Character Name");
            newPrintWriter.println("Character Level");
            newPrintWriter.println("Character Attack");
            newPrintWriter.println("Character Potions");
            newPrintWriter.println("Everything Else");
            newPrintWriter.close();
    }
}


Try that.
rizzix




PostPosted: Sat Jan 22, 2005 1:36 pm   Post subject: (No subject)

wtd wrote:
First off, import the java.lang package so you can use the String class.

code:
import java.lang.*;





You dont have to explicitly import java.lang.* cuz its is always imported automatically.


And.. Hikaru79, his way is actually the better way of going about with it. (it prevents file corruption in some OS's)
wtd




PostPosted: Sat Jan 22, 2005 4:18 pm   Post subject: (No subject)

rizzix wrote:
wtd wrote:
First off, import the java.lang package so you can use the String class.

code:
import java.lang.*;





You dont have to explicitly import java.lang.* cuz its is always imported automatically.


Assumptions are for the kind of programmers who brought you every last hole in Windows. Smile
rizzix




PostPosted: Sat Jan 22, 2005 6:01 pm   Post subject: (No subject)

oh.. dont worry about that! java is standardized (i.e it is required to have the java.lang.* always imported by default in every java implentation). hence a java programmer would "know" it.. not "assume" it. Wink
wtd




PostPosted: Sat Jan 22, 2005 6:17 pm   Post subject: (No subject)

There is no Java standard, aside from the de facto one imposed by Sun. Any implementation can feel free to not import java.lang by default. They just can't use the Java trademark if they do. It's sadly ironic that Microsoft of all companies actually put their high-level object-oriented language up for international standardization.

It's one line of code that future-proofs software. There's no good reason not to include it.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sat Jan 22, 2005 6:30 pm   Post subject: (No subject)

there is a "Java" standard.. as u said its imposed by Sun.

if a language does not follow up to the standard it is not Java.. and as i was saying.. in "Java" (not a java-look-alike), it is required to have the java.lang.* imported by default

"Every compilation unit automatically and implicitly imports every public type name declared by the predefined package java.lang, so that the names of all those types are available as simple names, as described in §7.5.3."
Hikaru79




PostPosted: Sat Jan 22, 2005 6:32 pm   Post subject: (No subject)

rizzix wrote:

And.. Hikaru79, his way is actually the better way of going about with it. (it prevents file corruption in some OS's)


The code in the try{} block cannot throw an IOException error. At least, according to either of our compilers it cannot.

code:
hikaru79@ubuntu:~/Java/Testing $ javac Testing.java
Testing.java:18: exception java.io.IOException is never thrown in body of corresponding try statement
        catch (IOException e)
        ^
1 error


So either it's the wrong exception, or no exception is thrown. Either way, at the version I posted compiles, right? Wink
rizzix




PostPosted: Sat Jan 22, 2005 6:34 pm   Post subject: (No subject)

yea if it can't.
Hikaru79




PostPosted: Sat Jan 22, 2005 6:34 pm   Post subject: (No subject)

Actually, I just checked the Java API. In reference to PrintWriter, it says
Quote:
Methods in this class never throw I/O exceptions. The client may inquire as to whether any errors have occurred by invoking checkError().
rizzix




PostPosted: Sat Jan 22, 2005 6:36 pm   Post subject: (No subject)

ah! there we go.
Hikaru79




PostPosted: Sat Jan 22, 2005 6:46 pm   Post subject: (No subject)

Oh and guys, we forgot to use Syntax highlighting! Rizzix, you should add it to wtd's and Fiend's post. Beautify the threads =)
wtd




PostPosted: Sat Jan 22, 2005 6:50 pm   Post subject: (No subject)

Hikaru79 wrote:
Oh and guys, we forgot to use Syntax highlighting! Rizzix, you should add it to wtd's and Fiend's post. Beautify the threads =)


Problem is, last I checked, it doesn't put everything in a
 block, so t's not using a monospace font, which would make it harder to read.  Smile
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: