java IO 读取数据

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();
    }
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,769评论 18 399
  • 最不希望到来的一个周末还是到来了,周五晚上,我翻箱倒柜找出当年出席毕业晚会的礼服,楚悦也大概猜到了一二,问,「如果...
    代码胖阅读 255评论 0 1
  • 停滞在眼前 脚不再前行 一动不动 不要灰心 拿起手边的书 捧一杯香茗 阳光倾泻 宁静而悠远 找寻黄金屋 邂逅颜如玉...
    纤夏阅读 116评论 0 0
  • 听说,你那边今天下雪了。 一直觉得下雪的时候很浪漫,一夜白头,青丝变白发也是极美的。可是我知道,这场雪对你来说...
    晨定阅读 213评论 1 2