简体   繁体   English

使用Java读取.txt文件?

[英]Reading .txt file with Java?

I am creating the following program, which reads a text.file and print out certain things based on the arguments that are given. 我正在创建以下程序,该程序读取text.file并根据给定的参数打印出某些内容。 If the user inputs "run Profile text.txt", I would like it to print out the file line by line. 如果用户输入“运行配置文件text.txt”,我希望它逐行打印出文件。 If the user inputs "run Profile text.txt 5", the first 5 lines should be printed out. 如果用户输入“ run Profile text.txt 5”,则应该打印出前5行。 I have the following program written: 我编写了以下程序:

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

public class Profile{

  public static String file;
  public static int len;
  public static Profile a;
  public static Profile b;

  //Method to read whole file
  static void wholeFile(String file){
    Scanner in = new Scanner(file);
    int lineNumber = 1;

    while(in.hasNextLine()){
      String line = in.nextLine();
      System.out.println("/* " + lineNumber + " */ " + line);
      lineNumber++;
    }
    in.close();
  }

  //Method to read file with line length
  static void notWholeFile(String file, int len){
    Scanner in = new Scanner(file);
    int lineNumber = 1;

    while(in.hasNextLine() && lineNumber <= len){
      String line = in.nextLine();
      System.out.println("/* " + lineNumber + " */ " + line);
      lineNumber++;
    }
    in.close();
  }

Profile(String file){
    this.file = file;
}
Profile(String file, int len){
    this.file = file;
    this.len = len;
    notWholeFile(file, len);
}
  public static void main(String[] args){
    Scanner in = new Scanner (System.in);
    if (args.length == 1){
      file = args[0] + "";
      a = new Profile(file);
      wholeFile(file);
    }     
    if (args.length == 2){
      file = args[0] + "";
      len = Integer.parseInt(args[1]);
      b = new Profile(file, len);
      notWholeFile(file, len);
    }   
  }
}

For testing purposes, I have included a .txt file in my directory by the name of "text.txt", which contains the following text: 出于测试目的,我在目录中包含一个名为“ text.txt”的.txt文件,其中包含以下文本:

blah blah blah blah blah blah blah
blah blah blah blah blah blah blah

blah blah blah blah blah blah blah
blah blah blah blah blah blah blah

blah blah blah blah blah blah blah
blah blah blah blah blah blah blah

blah blah blah blah blah blah blah
blah blah blah blah blah blah blah

I am a beginner with java but believe there shouldn't be any mistakes. 我是Java的初学者,但我相信不应有任何错误。 However, when I input "run Profile text.txt 5", I get the following output: 但是,当我输入“ run Profile text.txt 5”时,将得到以下输出:

> run Profile text.txt 5
/* 1 */ text.txt
/* 1 */ text.txt
> 

Why can I not get the "blah blah" lines to print out? 为什么不能打印出“ blah blah”行? Is there an error with the way I am reading the .txt file? 我读取.txt文件的方式是否存在错误? How can I access the lines inside of this text file? 如何访问此文本文件中的行? Any advice will be helpful. 任何建议都会有所帮助。

You are scanning the name of the file, not the contents of the file. 您正在扫描文件的名称 ,而不是文件的内容 That is: 那是:

Scanner in = new Scanner(file);  // where file is of type string

creates a Scanner that reads from the String itself. 创建一个从String本身读取的Scanner Try something like: 尝试类似:

Scanner in = new Scanner(new File(file));

This should read the contents of the file. 这应该读取文件的内容。

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

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