Java BufferedReader Read All readAll(String ip)

Here you can find the source of readAll(String ip)

Description

read All

License

Open Source License

Declaration

public static List<String> readAll(String ip) 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static ConcurrentHashMap<String, Object> monitor = new ConcurrentHashMap<String, Object>();
    private static String rootPath;
    private static final String CHARSET = "UTF-8";

    public static List<String> readAll(String ip) {
        if (ip == null) {
            return null;
        }/*from  w ww  .j a  v  a  2 s .c  om*/
        BufferedReader br = null;
        List<String> result = new ArrayList<String>();
        File file = null;
        //      Object monitor1 = monitor.putIfAbsent(ip, new Object());
        synchronized (monitor) {
            try {
                file = new File(createFileNameByIp(ip));
                if (!file.exists()) {
                    return null;
                }
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file), CHARSET));
                String line = null;
                while ((line = br.readLine()) != null) {
                    if (!"".equals(line)) {
                        result.add(line);
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                    file.delete();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    private static String createFileNameByIp(String ip) {
        ip = ip.replace(".", "_");
        String filePath = rootPath + File.separator + ip + ".txt";
        System.out.println("filePaht:" + filePath);
        return filePath;
    }
}

Related

  1. readAll(Reader reader)
  2. readAll(String fileName)
  3. readAll(String fileName)
  4. readAll(String filePath)
  5. readAll(String filePathAndName)
  6. readAll(String path)
  7. ReadAll_Variant2(Reader rd, Writer wr)
  8. readAllCourseNum()
  9. readAllCoursePro()