7

How does one use a specified file path rather than a file from the resource folder as an input or output stream? This is the class I have and I would like to read from a specific file path instead of placing the txt file in the resources folder in IntelliJ. Same for an output stream. Any help rendered would be appreciated thanks.

Input Stream

import java.io.*;
import java.util.*;

public class Example02 {
    public static void main(String[] args) throws FileNotFoundException {
        // STEP 1: obtain an input stream to the data

        // obtain a reference to resource compiled into the project
        InputStream is = Example02.class.getResourceAsStream("/file.txt");

        // convert to useful form
        Scanner in = new Scanner(is);

        // STEP 2: do something with the data stream
        // read contents
        while (in.hasNext()) {
            String line = in.nextLine();
            System.out.println(line);
        }

        // STEP 3: be polite, close the stream when done!
        // close file
        in.close();
    }
}

Output stream

import java.io.*;

public class Example03
{
    public static void main(String []args) throws FileNotFoundException
    {
        // create/attach to file to write too
        // using the relative filename will cause it to create the file in
        // the PROJECT root
        File outFile = new File("info.txt");

        // convert to a more useful writer
        PrintWriter out = new PrintWriter(outFile);

        //write data to file
        for(int i=1; i<=10; i++)
            out.println("" + i + " x 5 = " + i*5);

        //close file - required!
        out.close();            
    }
}
1
  • 2
    FileInputStream Commented Oct 13, 2017 at 6:46

3 Answers 3

21

Preferred way for getting InputStream is java.nio.file.Files.newInputStream(Path)

try(final InputStream is = Files.newInputStream(Paths.get("/path/to/file")) {
    //Do something with is
}

Same for OutputStream Files.newOutputStream()

try(final OutputStream os = Files.newOutputStream(Paths.get("/path/to/file")) {
    //Do something with os
}

Generally, here is official tutorial from Oracle to working with IO.

Sign up to request clarification or add additional context in comments.

Comments

0

First you have to define the path you want to read the file from, absolute path like so:

String absolutePath = "C:/your-dir/yourfile.txt"
InputStream is = new FileInputStream(absolutePath);

It's similar for writing the file as well:

String absolutePath = "C:/your-dir/yourfile.txt"
PrintWriter out = new PrintWriter(new FileOutputStream(absolutePath));

Comments

-1

You can use a file object as:

File input = new File("C:\\[Path to file]\\file.txt");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.