如何使用nextline方法使我的程序从文件读取行 [英] How do I make my program read lines from a file with the nextline method

查看:352
本文介绍了如何使用nextline方法使我的程序从文件读取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我完成了编码书中的代码,一切似乎都运行良好。我的代码问我文件的名称,我给了它。但后来我得到了所有这些红色。我搜索谷歌,但我得到的是一些视频游戏的答案。

继承我的代码。我正在使用eclipse。

So I did a code from my coding book perfectly and everything seemed to be working fine. My code asked me the name of the file and I gave it. But then I got all this red. I searched google but all I got was some video game answers.
Heres my code. I am using eclipse.

package TheDrive;

import java.io.*;
import java.util.Scanner;
class ReadFirstLine
{

	private static Scanner keyboard;

	public static void main(String[] args) throws IOException 
	{
		// Create a Scanner object for keyboard input
		keyboard = new Scanner(System.in);
		
		
		// Get the file name
		System.out.print("Enter the name of file: ");
		String filename = keyboard.nextLine();
		
		// Open the file
		File file = new File(filename);
		Scanner inputFile = new Scanner(file);
		
		// Read the first line from the file
		String line = inputFile.nextLine();
		
		// Display the line.
		System.out.println("The first line in the file is:");
		System.out.println(line);
		
		// Close the file.
		inputFile.close();
	}

}





我尝试过:



我尝试过从Eclipse更改为jGrasp,但我仍然遇到错误。



What I have tried:

I've tried changing from Eclipse to jGrasp but I still get errors.

推荐答案

你的代码编译而且运作良好。

可能你只是遇到了你最喜欢的 IDE 的麻烦。

尝试编译它并使用命令控制台进行激活。
Your code compiles and works well.
Probably you are just experiencing troubles with your favourite IDE.
Try to compile it and excuting using the command console.


这篇关于如何使用nextline方法使我的程序从文件读取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆