- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码:
Scanner scanner = new Scanner (file);
while (scanner.hasNextLine()){
System.out.println ("Next line");
String line = scanner.nextLine();
}
起初打印语句从未执行过。我认为这很奇怪,因为该文件是一个包含大约 2,000 行的 xml 文件。为了好玩,我尝试在初始化 Scanner 对象之前插入以下内容:
try{
Desktop.getDesktop().open(file);
} catch (Exception e){}
当我运行程序时,文件被打开并且打印语句执行了 2,000 次(意味着 scanner.hasNextLine() 有效)。我认为这是与权限有关的问题,所以注释掉上面的代码并尝试:
file.setWritable(true);
file.setExecutable(true);
file.setReadable(true);
scanner.hasNextLine() 回到不工作状态,打印语句从未执行。我做错了什么?
更新:
整个函数如下所示:
public void addToList (File file){
try{
file.setWritable(true);
file.setExecutable(true);
file.setReadable(true);
if (file.exists())
System.out.println("File exists");
Scanner scanner = new Scanner (file, "UTF-8");
int lineCount=0;
while (scanner.hasNextLine()){
System.out.println("Next line");
String line = scanner.nextLine();
}
}
catch (FileNotFoundException e){
System.err.println (e.getMessage());
}
}
其中文件是绝对路径
最佳答案
您使用了 try
,但从未捕获到任何东西,这可能是问题所在。当我添加一个 catch 并使用 xml
文件运行此代码时,它运行良好:
public static void main(String[] args) {
addToList(new File("/Users/[YourName]/Desktop/[FileName].xml"));
}
public static void addToList (File file){
try{
file.setWritable(true);
file.setExecutable(true);
file.setReadable(true);
if (file.exists())
System.out.println("File exists");
Scanner scanner = new Scanner (file, "UTF-8");
int lineCount=0;
while (scanner.hasNextLine()){
System.out.println("Next line");
String line = scanner.nextLine();
}
} catch (Exception e) {
System.out.println(e);
}
}
关于java - Scanner.hasNextLine() 方法看不到文件中的下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45202104/
scanner.Scanner 之间有什么区别?来自包裹text/scanner ,和一个 bufio . Scanner ? 最佳答案 text/scanner更适合阅读源代码,主要是 Go 源代码
我有以下代码: Scanner in = new Scanner (System.in); String[] data = new String[5]; System.out.println("Ple
我有 Java 代码,它要求用户输入,然后将此数据存储在字符串变量中。下面的函数是“number”类的一部分,并在主函数中调用。 public static void setVal(i
我正在编写一个java程序,它运行一个循环并不断询问用户输入。然后程序用该字符串执行一系列操作,并请求另一个字符串并重复。 问题是许多字符串非常相似,所以我想用循环中上次的输入填充提示。例如:如果用户
我在 O'Reillys Java Cookbook(第 2 版)中寻找一些好东西,发现 Scanner.create() 方法大约 10 次。但是在 API 或类声明\实现中没有这样的东西。例如:P
这个问题在这里已经有了答案: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个答案) 关闭 4 年前。 im
这样做有什么好处/坏处吗? 通常,从流中读取时会抛出异常: try { inputStream.read(); }catch(IOException e) { e.printStack
Scanner console=new Scanner(System.in); System.out.print("how many:"); int n=console.nextInt(); cons
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 6 年前。 我最近刚刚
Scanner input = new Scanner(System.in); 你能详细解释一下上面的代码一步一步做了什么吗?我真的不明白它是如何工作的以及它如何链接到我以后能够做这个声明: int
这个问题在这里已经有了答案: Scanner is skipping nextLine() after using next() or nextFoo()? (24 个答案) 关闭 6 年前。 我必
如果我在 java.util 上调用 scanner.hasNext(pattern),然后使用相同的模式调用 scanner.next(pattern),我会扫描两次吗?扫描仪 假设我有很多案例的这
这是我的问题: 我正在尝试使用 Scanner 和 System.in 从键盘获取输入并将其分配给 int 变量。 这就是我所拥有的(完整的程序如下): // this program will us
使用 try(Scanner scan = new Scanner(System.in)) { } 导致 Exception in thread "main" java.util.NoSuchElem
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
此问题仅用于教育目的。我从 Java 教科书中获取了以下代码,我很好奇为什么在 catch block 中使用了 input.nextLine()。 我尝试使用 input.nextInt() 代替它
这个问题已经有答案了: How to use java.util.Scanner to correctly read user input from System.in and act on it?
我的教授倾向于执行以下操作以从用户那里获取数字: Scanner scanner = new Scanner(System.in); Integer.parseInt(scanner.nextLine
我在使用 Scanner 时出现奇怪的行为。当我使用 Scanner(FileInputStream) 构造函数时,它将与我正在使用的一组特定文件一起使用,但它不适用于 Scanner(File) 构
D 中是否有类似于 Java 扫描仪的流解析器?您可以去哪里nextInt()获取 int和 nextLong()对于 long , 等等。 最佳答案 std.conv.parse 类似: http:
我是一名优秀的程序员,十分优秀!