Java: File
import javafx.application.Application; //https://gluonhq.com/products/javafx/ https://github.com/openjdk/jfx
import javafx.fxml.FXMLLoader; //https://gluonhq.com/products/javafx/
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.*;
import java.nio.file.Files;
import java.nio.file.Files.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.lang.IllegalStateException;
Scanner input=new Scanner(System.in);
System.out.printf("Enter file or directory name:");
Path path=Paths.get("D:\\书籍资料\\java\\JavaHowToProgram11e_EarlyObjects-master\\examples");
if(Files.exists(path))
{
System.out.println("FileName:"+path.getFileName());
if(Files.isDirectory((path)))
{
try {
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
for (Path p : directoryStream) {
System.out.println(p.toString());
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
}
/**/
try(Scanner duinput=new Scanner(Paths.get("clients.txt")))
{
System.out.printf("%-10s%-12s%-12s%10s%n", "Account","First Name", "Last Name", "Balance");
while (duinput.hasNext()) {
// System.out.printf(""+
System.out.printf("%-10d%-12s%-12s%10.2f%n", duinput.nextInt(), duinput.next(), duinput.next(), duinput.nextDouble());
//System.out.printf("geovindu");
}
}
catch (IOException|NoSuchElementException|IllegalStateException exception)
{
exception.printStackTrace();
}
//写文件
String dufileName="geovin.txt";
String value = "Hello,涂聚文,你好吗";
try {
FileOutputStream fos = new FileOutputStream(dufileName);
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
outStream.writeUTF(value);
outStream.close();
// verify the results
String result;
FileInputStream fis = new FileInputStream(dufileName);
DataInputStream reader = new DataInputStream(fis);
result = reader.readUTF();
reader.close();
}
catch (IOException exception)
{
exception.printStackTrace();
}
//写文件
Scanner geovinduinput = new Scanner(System.in);
System.out.printf("%s%n%s%n? ",
"Enter account number, first name, last name and balance.",
"Enter end-of-file indicator to end input.");
try (Formatter output = new Formatter("geovindu.txt")) {
while (geovinduinput.hasNext()) { //
try {
output.format("%d %s %s %.2f%n", geovinduinput.nextInt(),
geovinduinput.next(), geovinduinput.next(), geovinduinput.nextDouble());
}
catch (NoSuchElementException elementException) {
System.err.println("Invalid input. Please try again.");
geovinduinput.nextLine(); //
}
System.out.print("? 请输入下一行:");
}
}
catch (SecurityException | FileNotFoundException |
FormatterClosedException e) {
e.printStackTrace();
System.exit(1); //
}
//写文件
String dufileName="geovin1.txt";
String value = "Hello,涂聚文,你好吗";
StringBuilder builder=new StringBuilder();
builder.indexOf(value);
builder.indexOf("geovindu");
try {
FileOutputStream fos = new FileOutputStream(dufileName);
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
outStream.writeUTF(builder.toString());
outStream.close();
// verify the results
String result;
FileInputStream fis = new FileInputStream(dufileName);
DataInputStream reader = new DataInputStream(fis);
result = reader.readUTF();
reader.close();
}
catch (IOException exception)
{
exception.printStackTrace();
}
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号