将给定的字符串行打印到列中

时间:2014-06-08 15:23:52

标签: java

我有* .txt文件,第一行是名称,地址,邮件ID,第二行是值。我必须将它打印成两列,第一列是标题,第二列是使用Java的值。我该怎么做呢?

public class ReadFile1 {

    public static void main(String[] args) {
        BufferedReader br=null;
        String sCurrentLine = null;
        String delimiter = ",";
        String[] filetags;
        try {
            br = new BufferedReader(new FileReader("path\\Read.txt"));
            sCurrentLine = br.readLine();
            StringBuffer result = new StringBuffer();           
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        String line = null;
        try {
            line = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        filetags = line.split(delimiter);
        for(int i = 0;i < line.length(); i++)
        {
            System.out.println("****" +sCurrentLine);
            String[] s = line.split(",");
            for(int j = i-1; j<line.length();j++)
            {
                System.out.println("##############"+Arrays.toString(s));
            }
        }
    }
}

这就是我尝试过的。例如:我有一个文件说,

line1) name,email,mobile and second 
line2) john,j@abc.com,9876 
line3) max,max@xyz.com,1234  

现在,我需要打印:

name john
email john@abc.com                                                  
moblie 9876                                                  
name max                                                                
email max@xyz.com                                                  
mobile 1234 

2 个答案:

答案 0 :(得分:1)

以下是您可以获得所需内容的一种方式,它类似于您的尝试方式,但略微更精致。

文件:

 name,email,mobile and second
 john,j@abc.com,9876
 max,max@xyz.com,1234

代码:

    //File is on my Desktop
    Path myFile = Paths.get(System.getProperty("user.home")).resolve("Desktop").resolve("tester.txt");
    //Try-With-Resources so we autoclose the reader after try block
    try(BufferedReader reader = new BufferedReader(new FileReader(myFile.toFile()))){
        String[] headings = reader.readLine().split(",");//Reads First line and gets headings
        String line;
        while((line = reader.readLine()) != null){//While there are more lines
            String[] values = line.split(","); //Get the values
            for(int i = 0; i < values.length; i++){//For each value
                System.out.println(headings[i] + ":  " + values[i]);//Print with a heading
            }
        }
    } catch (IOException io) {
        io.printStackTrace();
    } 

祝你好运!

答案 1 :(得分:0)

这样的事情应该可以解决问题。

  1. 读取文件并将每一行存储在列表中。
  2. 遍历行列表
  3. 如果可以安全地假设第一行始终是标题行,请输入并将其存储在集合中。
  4. 对于其余行,请在逗号上拆分并使用拆分器数组的索引来引用标题列。
  5. List <String> lines = new ArrayList<String>();
    Scanner scanner = new Scanner(new File("FileName.txt"));
    while(scanner.hasNextLine()){
        String line = scanner.nextLine();
        lines.add(line);
    }
    scanner.close();
    
    int lineNo = 0;
    List <String> title = new ArrayList<String>();
    for(String line : lines){
        if(lineNo == 0){
            String [] titles = line.split(",");
            for(String t : titles){
                title.add(t);
            }
            lineNo++;
        }
        else{
            String input = line.split(",");
            for(int i = 0; i<input.length; i++){
                System.out.println(title.get(i) + ": " + input[i]);
            }
            lineNo++;
        }
    }