简体   繁体   English

来自文件路径的Java输入和输出流

[英]Java input and output stream from file path

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. 这是我拥有的课程,我想从特定的文件路径中读取,而不是将txt文件放在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();            
    }
}

Preferred way for getting InputStream is java.nio.file.Files.newInputStream(Path) 获取InputStream的首选方法是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() 与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. 通常,这是Oracle提供的与IO一起使用的官方教程

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));

您可以将文件对象用作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM