Home › Forums › General Chat › Java I/O
- This topic has 7 replies, 4 voices, and was last updated 14 years, 3 months ago by
Joaco.
-
AuthorPosts
-
24 October 2010 at 21:09 #3452
Arladerus
ParticipantCan anybody explain to me how the I/O syntax is used in Java?
Creating a file:
Path file = ...;
try {
file.createFile(); //Create the empty file with default permissions, etc.
} catch (FileAlreadyExists x) {
System.err.format("file named %s already exists%n", file);
} catch (IOException x) {
//Some other sort of failure, such as permissions.
System.err.format("createFile error: %s%n", x);
}Reading a file:
Path file = ...;
InputStream in = null;
try {
in = file.newInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException x) {
System.err.println(x);
} finally {
if (in != null) in.close();
}Writing a file:
import static java.nio.file.StandardOpenOption.*;
Path logfile = ...;
//Convert the string to a byte array.
String s = ...;
byte data[] = s.getBytes();
OutputStream out = null;
try {
out = new BufferedOutputStream(logfile.newOutputStream(CREATE, APPEND));
...
out.write(data, 0, data.length);
} catch (IOException x) {
System.err.println(x);
} finally {
if (out != null) {
out.flush();
out.close();
}
}25 October 2010 at 00:40 #21070DarkDragoon
Participantlolwut, what I use for general all purpose filereading etc.
import java.io.*;
//you know where that goes.
void classThatReadsShitorEtc() throws IOException{
//read that stuff in
BufferedReader file=new BufferedReader(new FileReader(" ")); //file name goes inside the quotes
file.readLine(); //and etc.
//Print shit out
PrintWriter outFile=new PrintWriter(new BufferedWriter(new FileWriter(" "))); //file name goes inside the quotes
out.print("stuff");
out.close();
}Yarr, might not be what you want though
25 October 2010 at 02:19 #21071David
ParticipantOh, that’s what you wanted to know?..
-_-
Did you get to exceptions yet though? Do you understand the try{ and catch error{ and finally{ parts? I don’t know if that’s confusing you or not.
25 October 2010 at 23:03 #21072DarkDragoon
ParticipantDavid said: Oh, that’s what you wanted to know?..-_-
Did you get to exceptions yet though? Do you understand the try{ and catch error{ and finally{ parts? I don’t know if that’s confusing you or not.
That would definitely throw him off if he didn’t know those.
26 October 2010 at 21:37 #21081Arladerus
ParticipantI don’t need it, but I got the try catch error finally parts. I just needed an example of what goes in the arguments. Thanks
27 October 2010 at 01:03 #21083David
Participanttry {
BufferedWriter out = new BufferedWriter(new FileWriter(“outfilename”));
out.write(“aString”);
out.close();
}
catch (IOException e) {
}You get that right? we’re creating a new BufferedWriter object, which is then going to write to a filename that is currently known as “outfilename”, and then we’re going to write in “aString” into that file, and then close the buffer.
We catch any exceptions and we don’t do anything with it.
And we end the block.
So all of the stuff after the ‘.’ are just methods included in the BufferedWriter class – they’re pre-written so you don’t have to. It just depends on what you use to do what.
I don’t know what you’re trying to accomplish still.
Like the same thing for reading:
What it does is it creates a new BufferedWriter object, then it creates a new string that’s set as null.
Then it reads the file that’s designated line per line, replaces the ‘null’ with that particular line, then spits it back out.The file path is often in the same folder, unless you specify a new location for it to look it.
27 October 2010 at 02:47 #21084DarkDragoon
ParticipantDavid said: try {
BufferedWriter out = new BufferedWriter(new FileWriter(“outfilename”));
out.write(“aString”);
out.close();
}
catch (IOException e) {
}You get that right? we’re creating a new BufferedWriter object, which is then going to write to a filename that is currently known as “outfilename”, and then we’re going to write in “aString” into that file, and then close the buffer.
We catch any exceptions and we don’t do anything with it.
And we end the block.
So all of the stuff after the ‘.’ are just methods included in the BufferedWriter class – they’re pre-written so you don’t have to. It just depends on what you use to do what.
I don’t know what you’re trying to accomplish still.
Like the same thing for reading:
What it does is it creates a new BufferedWriter object, then it creates a new string that’s set as null.
Then it reads the file that’s designated line per line, replaces the ‘null’ with that particular line, then spits it back out.The file path is often in the same folder, unless you specify a new location for it to look it.
This man speaks as much of the truth as we can give you without starting from the beginning.
13 November 2010 at 03:05 #21149Joaco
Participant
try {
try {
System.out.println("Enterprise Java");
} catch (IOException error) {
System.out.println("Almost had it");
}
} catch(e) {
System.out.println("Failed at trying");
} -
AuthorPosts
- You must be logged in to reply to this topic.