Java: File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
            }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//写文件
           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); //
            }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//写文件
          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 @   ®Geovin Du Dream Park™  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2016-02-19 csharp:正则表达式采集网页数据
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示