gpt4 book ai didi

Java 扫描器不读取

转载 作者:行者123 更新时间:2023-11-29 08:28:14 25 4
gpt4 key购买 nike

我正在尝试从远程计算机下载一个包含整数的文件,在本地增加整数的值,将新值写入同一文件并上传文件。我使用 scp。它成功下载了文件。我使用 shell 文件进行下载和上传过程。但是我在使用 Scanner 时遇到了问题。

代码如下:

import java.io.*;
import java.util.*;
public class shell {
public static void main(String[] args) throws IOException{
Runtime.getRuntime().exec("/home/ayyuce/Desktop/download.sh");
File f= new File("/home/ayyuce/Desktop/yeni.txt");
PrintWriter pw = new PrintWriter(f);
Scanner s= new Scanner(f);
int num=0;
if(s.hasNextLine()){
num=s.nextInt();
} else {
System.out.println("Error");
}
int increase=num++;
pw.println(increase);

Runtime.getRuntime().exec("/home/ayyuce/Desktop/upload.sh");
s.close();
pw.close();
}
}

输出是:错误

我想知道 Scanner 有什么问题。

非常感谢!

最佳答案

PrintWriter pw = new PrintWriter(f);

来自 javadoc

file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

当然,Scanner 无法读取任何内容,因为文件在 new PrintWriter(f) 中被截断为零大小。

关于Java 扫描器不读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50624503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com