java IO 读取数据

作者: 熬夜的猫头鹰 | 来源:发表于2018-06-16 23:33 被阅读0次

    java IO 读取数据

    提供集中读取数据的方法

    
    package com.viashare.read;
    
    import org.omg.CORBA.PRIVATE_MEMBER;
    
    import java.io.*;
    
    /**
     * Created by Jeffy on 16/5/17.
     */
    public class ReadMain {
    
        private static final String READ_PATH = "/Users/jeffy-pc/Downloads/test.txt";
        private static final String WRITE_PATH = "/Users/jeffy-pc/Downloads/test2.txt";
    
        public static void main(String[] args) throws IOException {
    //        bufferRead();
    //        ByteReader();
            randomRead();
        }
    
        private static final void bufferRead() throws IOException {
            BufferedReader input = new BufferedReader(new FileReader(READ_PATH));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while((line = input.readLine())!=null){
                sb.append(line);
            }
            if(input != null){
                input.close();
            }
            System.err.println(sb.toString());
        }
    
        private static final void ByteReader() throws IOException {
            File file = new File(READ_PATH);
            InputStream in = new FileInputStream(file);
            System.err.println(convertStreamToString(in));
            in.close();
        }
    
        static String convertStreamToString(java.io.InputStream is) {
            java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
            return s.hasNext() ? s.next() : "";
        }
    
        private static final  void randomRead() throws IOException {
            RandomAccessFile randomAccessFile = new RandomAccessFile(READ_PATH,"r");
            long length = randomAccessFile.length();
            randomAccessFile.seek(0);
            byte[] bytes = new byte[1024];
            int readByte = 0;
            while((readByte = randomAccessFile.read(bytes)) != -1){
                System.out.write(bytes, 0, readByte);
            }
            if(null != randomAccessFile){
                randomAccessFile.close();
            }
    
        }
    }
    
    

    java7 读取文件的API

    
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.nio.file.*;
    import java.util.List;
    
    /**
     * Created by jeffy on 2016/05/17.
     */
    public class QuickReadAndWrite {
    
        public static void main(String[] a) throws IOException {
            Path path= Paths.get("/Users/xieqiang/test.txt");
            try(
                //如果文件存在则直接打开,否则创建文件    
                BufferedWriter writer=Files.newBufferedWriter(path,Charset.forName("utf-8"));
    
                //可以指定打开文件的模式,这里表示追加文件
                //BufferedWriter writer=Files.newBufferedWriter(path,Charset.forName("utf-8"),StandardOpenOption.APPEND);
            ) {
                writer.write("hello,java7,我是不迷失");
                writer.newLine();
                writer.write("test");
                System.out.println("ok");
            }
    
    
            List<String> lines= Files.readAllLines(path);
            System.out.println(lines);
        }
    }
    
    
    
    
        private static final void readAndCopy() throws URISyntaxException, IOException {
            Files.lines(Paths.get(READ_PATH), StandardCharsets.UTF_8).forEach(System.out::println);
            BufferedWriter writer = Files.newBufferedWriter(Paths.get(WRITE_PATH), StandardCharsets.UTF_8);
            writer.write(new String(Files.readAllBytes(Paths.get(READ_PATH))));
            writer.flush();
        }
        
    

    相关文章

      网友评论

        本文标题:java IO 读取数据

        本文链接:https://www.haomeiwen.com/subject/bjrieftx.html