的ReadLine();在这里不起作用 [英] Readline(); doesn't work here

查看:100
本文介绍了的ReadLine();在这里不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

public class Test {
	
	public static void main ( String[] args) {
		
		Scanner input = new Scanner(System.in);
		System.out.println("Enter name of input file: ");
		String fileName = input.nextLine();
	
		
		String text = " ";
		String line = fileName.readLine();
		while ( line != null)
		{
			text += line ;
			line = fileName.readLine();
		}
		
		System.out.print(text);
	}

}





我尝试过:



它说String没有名为readLine()的函数



What I have tried:

it says String doesn't have function called readLine()

推荐答案

那是因为filename是定义为String。您不对文件名执行文件操作。在您的情况下,您使用文件名来打开FileInputStream。然后你可以在那个流上调用readline()。



通过本教程 [ ^ ]因为你'我将不得不重写你的代码。
That's because "filename" is defined as a String. You don't do a file operation on a filename. In your case, you use the filename to open a FileInputStream. You then can call readline() on that stream.

Go through this tutorial[^] because you're going to have to rewrite your code a little bit.


这篇关于的ReadLine();在这里不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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