我在其他包中有其他名为Test的类,在默认包中有一个同名的类.
当我单击Eclipse中的Run按钮而不是运行此类时,它会从另一个包中运行另一个Test类:
package jfx;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Test extends Application {
public void start(Stage stage) {
Circle circ = new Circle(40, 40, 30);
Group root = new Group(circ);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我正在尝试将一些文本写入文件.我有一个while循环,应该只需要一些文本并将完全相同的文本写回文件.
我发现永远不会输入while循环,因为Scanner认为没有更多文本可供阅读.但是还有.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class WriteToFile {
public static void main(String[] args) throws FileNotFoundException {
String whatToWrite = "";
File theFile = new File("C:\\test.txt");
Scanner readinput = new Scanner(theFile);
PrintWriter output = new PrintWriter(theFile);
while (readinput.hasNext()) { //why is this false initially?
String whatToRead = readinput.next();
whatToWrite = whatToRead;
output.print(whatToWrite);
}
readinput.close();
output.close();
}
}
Run Code Online (Sandbox Code Playgroud)
文本文件只包含随机单词.狗,猫等
当我运行代码时,text.txt变为空.
有一个类似的问题:https://stackoverflow.com/questions/8495850/scanner-hasnext-returns-false指出编码问题.我使用Windows 7和美国语言.我能以某种方式找出文本文件的编码方式吗?
更新:
事实上,正如Ph.Voronov评论的那样,PrintWriter系列会删除文件内容!user2115021是正确的,如果你使用PrintWriter,你不应该在一个文件上工作.不幸的是,对于我必须解决的任务,我不得不使用单个文件.这是我做的:
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException; …Run Code Online (Sandbox Code Playgroud)