
-----------------------------------
Fiend-Master
Fri Jan 21, 2005 9:50 pm

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:

import java.io.*;

public class Testing
{
    public static void main (String args

-----------------------------------
wtd
Fri Jan 21, 2005 10:18 pm


-----------------------------------
First off, import the java.lang package so you can use the String class.

import java.lang.*;

Next, you can get rid of the extra variables.

import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main (String args

Next, variable names should begin with lowercase letters, and there's no need for spaces between function names and parentheses.

import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main(String args

Oh, and "String argsimport java.io.*;
import java.lang.*;

public class Testing
{
    public static void main(String

The following doesn't serve any purpose at all, except to make the program pause.

info

Oh, and if we have an error message, we should be printing it to the error handle.  :)

And also a bunch of variables here are useless, so we can eliminate them.

import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main(String

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
Sat Jan 22, 2005 10:09 am


-----------------------------------
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
Sat Jan 22, 2005 12:30 pm


-----------------------------------
import java.io.*;
import java.lang.*;

public class Testing
{
    public static void main(String

Try that.

-----------------------------------
rizzix
Sat Jan 22, 2005 1:36 pm


-----------------------------------
First off, import the java.lang package so you can use the String class.

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
Sat Jan 22, 2005 4:18 pm


-----------------------------------
First off, import the java.lang package so you can use the String class.

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.  :)

-----------------------------------
rizzix
Sat Jan 22, 2005 6:01 pm


-----------------------------------
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
Sat Jan 22, 2005 6:17 pm


-----------------------------------
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.

-----------------------------------
rizzix
Sat Jan 22, 2005 6:30 pm


-----------------------------------
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

Java Language Specification

-----------------------------------
Hikaru79
Sat Jan 22, 2005 6:32 pm


-----------------------------------

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.

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? ;)

-----------------------------------
rizzix
Sat Jan 22, 2005 6:34 pm


-----------------------------------
yea if it can't.

-----------------------------------
Hikaru79
Sat Jan 22, 2005 6:34 pm


-----------------------------------
Actually, I just checked the Java API. In reference to PrintWriter, it says
 Methods in this class never throw I/O exceptions. The client may inquire as to whether any errors have occurred by invoking checkError().

-----------------------------------
rizzix
Sat Jan 22, 2005 6:36 pm


-----------------------------------
ah! there we go.

-----------------------------------
Hikaru79
Sat Jan 22, 2005 6:46 pm


-----------------------------------
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
Sat Jan 22, 2005 6:50 pm


-----------------------------------
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.  :)

-----------------------------------
rizzix
Sat Jan 22, 2005 6:51 pm


-----------------------------------
sure thing. although the staff has decided not to impose rules to the usage of [syntax] highlighting.. but one thing's for sure.. all tutorials (well at least in the java section) are and will always be [syntax] highlighted
