从/向路径读取和写入CSV

时间:2018-05-21 11:24:50

标签: java android file csv input

我想逐行打印位于某个目录中的文件:

private void readWeatherDataByColumn() {

    FileInputStream is = null;

    try {
        is = new FileInputStream(sourceDirectory);

        String line = "";

        BufferedReader br = new BufferedReader(
                new InputStreamReader(is, "UTF-8"));

        while ((line = br.readLine()) != null) {

            System.out.println(line);
        }
    } catch (IOException e) {
        // Prints throwable details
        e.printStackTrace();
    }
}

我得到以下输出:

  

05-21 20:13:42.018 4170-4170 / com.soialab.askaruly.camera_sensor I /System.out: ftypisom isomiso2avc1mp41

任何人都有线索吗?

必须输出

  

05-22 17:13:22.676 5955-5955 / com.soialab.askaruly.camera_sensor I / System.out:1,22:28:23,42,92,66,224,40,0.28,0.02,0.05   05-22 17:13:22.677 5955-5955 / com.soialab.askaruly.camera_sensor I / System.out:2,22:28:24,48,92,191,224,64,0.28,0.02,0.05

2 个答案:

答案 0 :(得分:1)

  

将以下代码添加到您要读取CSV文件的位置。

String csvFileString = readFile(selectedFile.getAbsolutePath()); // path of you selected CSV File
InputStream stream = null;
try {
            stream = new ByteArrayInputStream(csvFileString.getBytes(StandardCharsets.UTF_8.name()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
 }
ReadCsv csv = new ReadCsv(stream);
List<String[]> results = new ArrayList<String[]>();
results = csv.read();



 public static String readFile(String theFilePathString) {
        String returnString = "";
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream((theFilePathString)), "UTF8"));
            String line = null;
            StringBuilder stringBuilder = new StringBuilder();
            String ls = System.getProperty("line.separator");

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append(ls);
            }
            reader.close();
            returnString = stringBuilder.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnString;
    }
  

ReadCsv.Class

public class ReadCsv {
    InputStream in;

    public ReadCsv(InputStream in) {
        this.in = in;
    }

    public List<String[]> read() {
        List<String[]> results = new ArrayList<String[]>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] row = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
                results.add(row);
            }
        } catch (IOException e) {
            throw new RuntimeException("Error reading CSV File " + e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                throw new RuntimeException("Error closing inputstream " + e);
            }
        }

        return results;
    }
}

答案 1 :(得分:0)

感谢您的评论和回复!
我解决了这个问题。字符串 sourceDirectory 是视频文件,而不是原始&#34; .csv&#34;文本文件。因此,正如@TimBiegeleisen所提到的那样,出现了一些编码问题 现在,它使用相同的代码完全正常。我很难过,抱歉......