• java: file


    /**
     * 版权所有 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

       /**
         *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;
        }
    

      

  • 相关阅读:
    单相、二相、三相区别
    Live for Speed 车模、赛道模型导出
    Lenovo/IBM Thinkpad X41 Tablet
    科普题外话 赛车性能的关键指标: 马力和扭力
    MAME™ Official Developer Documentation!
    Visual Studio 2005 & SQL Server 2005 are COMING!
    星际争霸(Star Craft)的Sprites导出
    推荐一本关于操作系统实践的好书
    科普题外话:Experimental Advanced Superconducting Tokamak - 人造太阳
    The Space Elevator -通往地球同步轨道的天梯
  • 原文地址:https://www.cnblogs.com/geovindu/p/15818402.html
Copyright © 2020-2023  润新知