Posted: 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.*;
publicclass Testing
{ publicstaticvoid main (String args[])throwsIOException { String[] info = newString[5];
String empty = "";
int temp = 0;
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*/
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*/
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*/
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
Posted: 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
Posted: Sat Jan 22, 2005 12:30 pm Post subject: (No subject)
Posted: 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
Posted: 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.
rizzix
Posted: 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.
wtd
Posted: 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
rizzix
Posted: 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
Posted: 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?
rizzix
Posted: Sat Jan 22, 2005 6:34 pm Post subject: (No subject)
yea if it can't.
Hikaru79
Posted: 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
Posted: Sat Jan 22, 2005 6:36 pm Post subject: (No subject)
ah! there we go.
Hikaru79
Posted: 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
Posted: 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.