【问题标题】:How to read a txt file as my system.in using eclipse如何使用 Eclipse 将 txt 文件作为我的 system.in 读取
【发布时间】:2015-05-14 22:18:01
【问题描述】:

我正在解决关于代码厨师的问题。我遇到了一个问题,它所说的只是我有错误的答案。我想测试我的程序以查看其输出,但它从文本文件中读取输入,我无法弄清楚如何使用 eclipse 来执行此操作,我的代码如下:

import java.io.*;
class Holes {

public static void main(String[] args) throws IOException{
    // TODO Auto-generated method stub
    BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
    int testCases = Integer.parseInt(r.readLine());

    for (int i =0; i<testCases; i++)
    {
        int holes = 0;
        String s = r.readLine();
        for (int j= 0; j< s.length(); j++)
        {
            char c = s.charAt(j);
            if (c == 'B')
                holes += 2;
            else if (c== 'A' || c== 'D' ||c== 'O' ||c== 'P' ||c== 'Q' ||c== 'R' )
            {
                holes +=1;
            }
            System.out.println(holes);
        }
    }   
}

}

【问题讨论】:

  • 2008 年有一个类似的问题,基本上说最好的方法是使用命令行,但我希望从那时起 eclipse 已经添加了该功能。
  • 看看filereader和scanner for file i/o。 stackoverflow.com/questions/4716503/…
  • 我使用 System.in 而不是 console.in??

标签: java eclipse io


【解决方案1】:

将文件夹添加到该文件夹​​中的 Eclipse 项目中,添加输入文件,然后使用 BufferReader 读取它,如下所示 BufferedReader br = null;

try {

    String sCurrentLine;

    br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt"));

    while ((sCurrentLine = br.readLine()) != null) {
        System.out.println(sCurrentLine);
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (br != null)br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

这是一种方式,另一种方式是将路径作为参数传递给程序 如下图所示

try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader(args[0]));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

如何在运行应用程序时执行运行配置,在那里你会发现 args,你可以在其中添加任何路径,例如 c:\myinput.txt 希望这会有所帮助

【讨论】:

  • 所以使用你的第一个例子我会改变: BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); to: br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt"));当然要更改目录。
【解决方案2】:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("C:\\testing.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2021-08-23
    相关资源
    最近更新 更多