小编Tor*_*que的帖子

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

如何使用指定的文件路径而不是资源文件夹中的文件作为输入或输出流?这是我的类,我想从特定的文件路径读取,而不是将txt文件放在IntelliJ的资源文件夹中.输出流相同.任何提供的帮助将不胜感激.

输入流

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 …
Run Code Online (Sandbox Code Playgroud)

java inputstream

0
推荐指数
1
解决办法
5255
查看次数

标签 统计

inputstream ×1

java ×1