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();
            }

  

posted @ 2023-02-19 22:14  ®Geovin Du Dream Park™  阅读(13)  评论(0编辑  收藏  举报