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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * IDE:IntelliJ IDEA 2021.2.3
 * 数据库:MSSQL Server 2019
 * OS:windows 10 x64
 * 历史版本: JDK 14.02
 * 2022-1-12 创建者 geovindu
 * 2022-1-15 添加 Lambda
 * 2022-1-15 修改:date
 * 接口类 mssql-jdbc-9.4.1.jre16.jar.
 *
 * 2022-1-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc FileHelper.java
 
 * */
 
 
 
package Geovin.Common;
 
import Geovin.Model.Person;
 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CountDownLatch;
 
 
/**
 *
 *
 *
 * */
public class FileHelper {
 
    /**
     * 读取文件流
     * @param fileName 文件
     * @return
     * */
    public BufferedReader bufferreader(String fileName) {
        BufferedReader reader = null; //使用缓冲区的方法将数据读入到缓冲区中
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reader;
    }
    /**
     * 读取文件中的某一行数据
     * @param reader
     * @param lineNumber
     * @return  string
     * */
    public String readfile(BufferedReader reader, int lineNumber) {
        String line = null; //定义行数
        try {
            int num = 0;
            while (true) {
                num++;
                line = reader.readLine();
                if (num == lineNumber) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return line;
        }
 
    }
    
    /**
     *
     *
     *
     * */
    public BigInteger getsize(String size){
        //System.out.println(size.substring(0,size.length()-1));
        int x=Integer.valueOf(size.substring(0,size.length()-1));
 
        BigInteger resultlen=new BigInteger("0");
        if(size.endsWith("m")||size.endsWith("M")) {
            resultlen = BigInteger.valueOf(x).multiply(BigInteger.valueOf(1024)).multiply(BigInteger.valueOf(1024));
        }else if(size.endsWith("g")||size.endsWith("G")){
            resultlen = BigInteger.valueOf(x).multiply(BigInteger.valueOf(1024)).multiply(BigInteger.valueOf(1024)).multiply(BigInteger.valueOf(1024));
        }else if(size.endsWith("K")||size.endsWith("k")){
            resultlen = BigInteger.valueOf(x).multiply(BigInteger.valueOf(1024));
        }
        return resultlen;
    }
 
    public synchronized static int  getuuid()
    {
        tmp=tmp+1;
 
        return tmp;
    }
    
 
    /**
     * 写数据
     * @param fw
     * @param size
     * @param list
     * @return
     *
     * */
    public boolean DuWriteFile(FileWriter fw, int size, List<Person> list) {
        DecimalFormat count=new DecimalFormat("00000000000");
        //经过测试:ufferedOutputStream执行耗时:1,1,1 毫秒
        int tmp_len=0;
        long begin0 = System.currentTimeMillis();
        try {
 
            String uuid=null;
            String uuid2=null;
            MessageDigest  md =MessageDigest.getInstance("MD5");
            LocalDate localdate=LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            String date=localdate.format(formatter);
            Random rd=new Random();
            int z=0;
 
            for(int i=0;i<size;i++){
 
                for(Person person:list) {
                    fw.write(person.getLastName()+","+person.toString());
                    System.out.println("ok");
                }
            }
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        } finally{
            long end0 = System.currentTimeMillis();
            System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) + " 毫秒");
        }
    }
     
     
     
    
 
    /**
     *复制文件
     * @param sourceFile  原文件
     * @param newFile  目标新文件
     *
     * */
    public void CopyFile(String sourceFile,String newFile)
    {
 
        try(FileInputStream inputStream=new FileInputStream(sourceFile);
            FileOutputStream outputStream=new FileOutputStream(newFile))
        {
            byte[] buffer=new byte[10];
            int len=inputStream.read(buffer);
            while (len!=-1)
            {
                String copyStr=new String(buffer);
                System.out.println(copyStr);
                outputStream.write(buffer,0,len);
                len=inputStream.read(buffer);
            }
 
 
 
        }
        catch (FileNotFoundException exception)
        {
            exception.printStackTrace();
        }
        catch (IOException exception)
        {
            exception.printStackTrace();
        }
 
    }
    /**
     *复制文件
     * @param sourceFile  原文件
     * @param newFile  目录新文件
     *
     * */
    public  void CopytFileDu(String sourceFile,String newFile)
    {
 
        try(FileInputStream inputStream=new FileInputStream(sourceFile);
            BufferedInputStream bufferedInputStream=new BufferedInputStream(inputStream);
            FileOutputStream outputStream=new FileOutputStream(newFile);
            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(outputStream)
            )
        {
            long startTime=System.nanoTime();
            byte[] buffer=new byte[1024];
            int len=bufferedInputStream.read(buffer);
            while(len!=-1)
            {
                bufferedOutputStream.write(buffer,0,len);
                len=bufferedInputStream.read(buffer);
            }
            long elapsedTime=System.nanoTime()-startTime;
            System.out.println("耗时:"+(elapsedTime/1000000.0)+" 毫秒");
 
        }
        catch (FileNotFoundException exception)
        {
            exception.printStackTrace();
        }
        catch (IOException exception)
        {
            exception.printStackTrace();
        }
 
 
    }
 
    /**
     * 删除文件
     * @param sroureFile
     *
     * */
    public  void Delete(String sroureFile)
    {
        try
        {
            File file=new File(sroureFile);
            file.deleteOnExit();
 
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
 
 
 
    }
 
 
 
}

  How to Read and Write Text File in Java (codejava.net)

https://www.codejava.net/java-se/file-io/how-to-read-and-write-text-file-in-java

 

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
/**
  *http://commons.apache.org/io/
  * You could use the Apache Commons IO library, which gives you FileUtils:    
  * FileUtils.writeStringToFile(file, stringBuilder.toString(), Charset.forName("UTF-8"))
  * @param stringBuffer
  * @param file
  * @return
  * */
 public boolean DuBuWriteFile(StringBuffer stringBuffer,File file)
 {
    boolean isok=false;
     try {
         FileUtils.writeStringToFile(file, stringBuffer.toString(), Charset.forName("UTF-8"));
         isok=true;
        }
     catch (Exception exception)
     {
         exception.printStackTrace();
     }
 
     return  isok;
 }
 /**
  *https://github.com/google/guava
  * Files.write(stringBuilder, file, Charsets.UTF_8)
  * @param stringBuffer
  * @param file
  * @return
  * */
 public boolean DuGoogleWriteFile(StringBuffer stringBuffer,File file)
 {
     boolean isok=false;
     try {
        com.google.common.io.Files.write(stringBuffer.toString(),file, Charsets.UTF_8);
         isok=true;
     }
     catch (Exception exception)
     {
         exception.printStackTrace();
     }
 
     return  isok;
 }

  

 

posted @   ®Geovin Du Dream Park™  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2019-01-18 wordcloud2.js
2016-01-18 javascript: jquery.gomap-1.3.3.js
2013-01-18 Csharp: Searching Within a String
2011-01-18 C# Menu 导航菜单控件Apple Safari & Google Chrome浏览器上显示无效问题
< 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
点击右上角即可分享
微信分享提示