如何使用Java将文本数据加载到表中

时间:2015-06-25 06:50:44

标签: java

我有像

这样的文字数据
name = abc

id = 123

Place = xyz

Details = some texts with two line



name = aaa

id = 54657

Place = dfd

Details = some texts with some lines

我需要将它们放在一个表或csv中,我的输出应该看起来像

name       id     Place       Details    

abc        123     xyz         Some texts

dfd        54657   dfd         Some texts  

我如何用java做到这一点?

3 个答案:

答案 0 :(得分:1)

  1. 使用Scanner(文档here
  2. 解析文本文件
  3. 创建DefaultTableModel(文档here)。 DefaultTableModel model = new DefaultTableModel(data, new String[]{"name","id","Place","Details"});,其中data是包含数据的2D字符串数组。
  4. 使用您刚创建的模型创建JTable(doc here)。 JTable table = new JTable(model);
  5. 使用JPanel(如果需要)将表格添加到JFrameJScrollPanepanel.add(new JScrollPane(table));

答案 1 :(得分:1)

使用java io可以实现从文本文件读取和写入csv(逗号分隔值)。

你的逻辑应该将标题写入一个文本文件,并将分隔符作为逗号,然后从文本中读取相应的值,可以使用split(“=”)并使用逗号分隔符附加到该文件。您可以创建新文件写入值并使用csv扩展名保存文件

try {
            BufferedReader bReader = new BufferedReader(new FileReader("input file"));
            String line = "";
            while ((line = bReader.readLine()) != null) {
                String[] strArray = line.split("=");
                // write this to file
                    System.out.println( strArray[1]);


                }
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

答案 2 :(得分:1)

CSV版本的代码:)它读取输入文件并以您要求的格式创建CSV:

<?xml version="1.0" encoding="UTF-8"?>
<engineers>
 <engineer engID="1" engName="John">
  <location fieldLoc="Geneva">
   <vistiDate>2015-01-23</vistiDate>
   <retDate>2015-06-28</retDate>
  </location>

  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
</engineer>

 <engineer engID="2" engName="Josh">
  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
 </engineer>

 <engineer engID="3" engName="Rita">
  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
 </engineer>
</engineers>

它处理的示例文件内容:

try {
            BufferedReader sc = new BufferedReader(new FileReader("input2.txt"));

            ArrayList<String> name = new ArrayList<>();
            ArrayList<String> id = new ArrayList<>();
            ArrayList<String> place = new ArrayList<>();
            ArrayList<String> details = new ArrayList<>();

            String line = null;
            while ((line = sc.readLine()) !=null) {
                if (!line.trim().equals("")) {
                    System.out.println(line);
                    if (line.toLowerCase().contains("name")) {
                        name.add(line.split("=")[1].trim());
                    }
                    if (line.toLowerCase().contains("id")) {
                        id.add(line.split("=")[1].trim());
                    }
                    if (line.toLowerCase().contains("location")) {
                        place.add(line.split("=")[1].trim());
                    }
                    if (line.toLowerCase().contains("details")) {
                        details.add(line.split("=")[1].trim());
                    }
                }
            }

            PrintWriter pr = new PrintWriter(new File("out.csv"));
            pr.println("name;id;Place;Details;");
            for (int i = 0; i < name.size(); i++) {
                pr.println(name.get(i) + ";" + id.get(i) + ";" + place.get(i) + ";" + details.get(i) + ";");
            }
            pr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }