使用 JAVA 控制台应用程序从桌面访问文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17107648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:01:13  来源:igfitidea点击:

Accessing a text file from desktop using JAVA console application

javafileconsole-applicationdesktop

提问by Vipin Nair

I have a text file in my desktop named "MyText.txt" and it have some contents in it.

我的桌面上有一个名为“MyText.txt”的文本文件,其中包含一些内容。

Since i am learning JAVA i thought of simply creating an console application which will read the contents from the txt file and then show it in console.

由于我正在学习 JAVA,我想简单地创建一个控制台应用程序,它将从 txt 文件中读取内容,然后在控制台中显示它。

But since i am new to this ,so i am unable to figure out the way it works.

但由于我是新手,所以我无法弄清楚它的工作方式。

Can anyone get me into a right direction.

任何人都可以让我进入正确的方向。

All help will be appreciated.

所有帮助将不胜感激。

采纳答案by Nandeshwar

You can try the following example to read file character basis.

您可以尝试以下示例来读取文件字符基础。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package io;

import java.io.*;

/**
*
* @author Nandeshwar
*/
public class FileExample {

 public static void main(String[] args) {
    File inFile = new File("c:/users/toshiba/desktop/MyText.txt");

    FileReader ins = null;

    try {
        ins = new FileReader(inFile);

        int ch;
        while ((ch = ins.read()) != -1) {
            System.out.println((char) ch);

        }
    } catch (Exception e) {
        System.out.println(e);
    } finally {
        try {
            ins.close();
        } catch (Exception e) {
        }
    }
 }

}

回答by pinkpanther

There are many alternatives for doing this. A higher level thing could be using a Scannerclass. I assume, since you are learning java, you might have come across Scannerclass for reading the input from console. You can use the Scannersame way to read the file also.

有很多替代方法可以做到这一点。更高层次的事情可能是使用一个Scanner类。我假设,由于您正在学习 Java,因此您可能遇到过从Scanner控制台读取输入的类。您也可以使用Scanner相同的方式读取文件。

You can use Scanner#nextInt(), Scanner#next(), etc... methods for reading the input. You can use args[]array for taking the command line arguments.

您可以使用Scanner#nextInt()Scanner#next()等... 方法来读取输入。您可以使用args[]数组来获取命令行参数。

Since, you haven't mention exactly what kind of way your data is stored in file, it's hard to give a working example.

因为,您还没有确切地提到您的数据以何种方式存储在文件中,所以很难给出一个有效的例子。

import java.util.Scanner;

public class Tester { 
  public static void main(String args[]) {
    if (args.length > 0) {
      Scanner sc = new Scanner(new File(args[0]));
      //use here the functions such as sc.nextInt() and so on
    }
  }
}

Link to the docs: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

链接到文档:http: //docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

If you are reading big file in java (see my answer to other question): file size too big for java:

如果您正在使用 java 读取大文件(请参阅我对其他问题的回答):文件大小对于 java 来说太大了

Working Example:

工作示例:

This program prints the file contents to console.

该程序将文件内容打印到控制台。

Execute this program using java Tester yourFileNameafter compilation.

java Tester yourFileName编译后使用执行此程序。

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

public class Tester { 
  public static void main(String args[]) {
    try {
      if (args.length > 0) {
        Scanner sc = new Scanner(new File(args[0]));
        while (sc.hasNext()) {
          System.out.println(sc.next());
        }
      } else {
        System.out.println("No file name given");
      }
    }
    catch(FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}

回答by zerocool

This should help. Create a BufferedReader object like this:

这应该有帮助。像这样创建一个 BufferedReader 对象:

new BufferedReader(new FileReader("Location of the file"));

new BufferedReader(new FileReader("文件位置"));

Read this doc, for reference:http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html

阅读此文档,以供参考:http: //docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html