Hdfs readline

转载地址:http://dacoolbaby.iteye.com/blog/1955427

一致疏于JAVA IO方面的基础学习。

这次在使用到HDFS的文件接口的时候,就遇到这方面的问题。

于是巩固一下基础。

java io是典型的decorator设计模式。

Path path = new Path(“路径”);

FSDataIOutputStream fdos = fs.append(path); //追加

fdos.write……即可

fdos.flush();//刷出数据

  

FSDataInputStream fdis = fs.open(path);

 

Text line = new Text();

LineReader reader = new LineReader(fdis); //一行一行的读 使用LineReader

while(reader.readLine(line) > 0) {

         System.out.println(line);//输出

fdis.close();

 

使用bufferedReader读取

 String uri = "hdfs://localhost:9000/hadoop/test/IP-COUNTRY-REGION-CITY.CSV.hive"; 

Configuration conf = new Configuration(); 
FileSystem fs = null; 
FSDataInputStream in = null; 
BufferedReader d = null; 

try { 
    fs = FileSystem.get(URI.create(uri), conf); 
    in = fs.open(new Path(uri)); 
    d = new BufferedReader(new InputStreamReader(in));   //使用BufferedReader 进行readLine
    String s = null; 
    int stat = 1; 
    while (true) { 
        System.out.println("line "+stat++); 
        s = d.readLine();

    public class JavaFile01 {  
      
    //InputStream 三个基本的读方法          
    //abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。          
    //int read(byte[] b) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。          
    //int read(byte[] b, int off, int len) :将数据读入一个字节数组,同时返回实际读取的字节数。  
    //如果返回-1,表示读到了输入流的末尾。  
    //off指定在数组b中存放数据的起始偏移位置;  
    //len指定读取的最大字节数。  
      
        public static void main(String[] args) throws IOException {  
      
        File f = new File("a.txt");  
        //InputStream是一个标准的输入接口  
        //对应读取数据的类有:FileInputStream ObjectInputStream等  
          
        //XXXInputStream就是为InputStream对象赋予额外的功能,  
        InputStream in = new BufferedInputStream(new FileInputStream(f));  
        byte[] b = new byte[1024];  
        while(in.read()!=-1){  
            try {  
            in.read(b);  
            System.out.println("读入buffer的信息:"+new String(b));  
            } catch (IOException e) {  
            e.printStackTrace();  
            }  
        }  
          
    //  InputSteream和OutpurStream是针对基于字节(byte)输入输出设计的,实际应用中常常需要读  
    //  写的是基于字符(char ,Unicode 2 个字节)的,java.io.Reader和java.io.Writer就是所有读  
    //  写字符数据流的父类。  
          
        //FileReader作为Reader对象的数据源  
        BufferedReader brin = new BufferedReader(new FileReader("a.txt"));  
          
          
        //Stream与Reader之间的转换  
        InputStreamReader isr = new InputStreamReader(in);//<--Stream输入  
        BufferedReader reader = new BufferedReader(isr);//使用Reader进行读取  
        System.out.println(reader.readLine());  
        }  
      
    }  
原文地址:https://www.cnblogs.com/yulijunzj/p/4289938.html