Java I/O

Home Forums General Chat Java I/O

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #3452
    Arladerus
    Participant

    Can 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();
    }
    }

    Source: download.oracle.com

    #21070
    DarkDragoon
    Participant

    lolwut, 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

    #21071
    David
    Participant

    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.

    #21072
    DarkDragoon
    Participant
    David 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.

    #21081
    Arladerus
    Participant

    I 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

    #21083
    David
    Participant

    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.

    #21084
    DarkDragoon
    Participant
    David 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.

    #21149
    Joaco
    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");
    }
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.