Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将给定的字符串行打印到列中_Java - Fatal编程技术网

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

Java 将给定的字符串行打印到列中,java,Java,我有*.txt文件,第一行是名称、地址、邮件id,第二行是值。我必须将其打印成两列,第一列带有标题,第二列带有使用Java的值。我该怎么做 public class ReadFile1 { public static void main(String[] args) { BufferedReader br=null; String sCurrentLine = null; String delimiter = ","; S

我有*.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));
            }
        }
    }
}
现在,我需要打印:

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

下面是一种你可以得到你想要的东西的方法,它与你尝试的方法相似,但稍微更精细一些

文件:

 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();
    } 
//文件在我的桌面上
Path myFile=Path.get(System.getProperty(“user.home”).resolve(“Desktop”).resolve(“tester.txt”);
//尝试使用资源,以便在尝试块后自动关闭读取器
try(BufferedReader=new BufferedReader(new FileReader(myFile.toFile())){
String[]headers=reader.readLine().split(,“”;//读取第一行并获取标题
弦线;
while((line=reader.readLine())!=null){//当有更多行时
String[]values=line.split(,“”;//获取值
对于(int i=0;i

祝你好运

像这样的事情应该可以奏效

  • 读取文件并将每一行存储在列表中
  • 遍历行列表
  • 如果可以安全地假设第一行始终是标题行,则获取输入并将其存储在集合中
  • 对于其余的行,使用逗号拆分,并使用拆分器数组的索引引用标题列
  • List line=new ArrayList();
    Scanner Scanner=新扫描仪(新文件(“FileName.txt”);
    while(scanner.hasNextLine()){
    字符串行=scanner.nextLine();
    行。添加(行);
    }
    scanner.close();
    int lineNo=0;
    列表标题=新建ArrayList();
    用于(字符串行:行){
    如果(行号==0){
    String[]titles=line.split(“,”);
    用于(字符串t:标题){
    标题.添加(t);
    }
    lineNo++;
    }
    否则{
    字符串输入=行。拆分(“,”);
    
    对于(inti=0;i1)。请提供输入和相应输出的示例。2.您尝试过什么?
        //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();
        } 
    
    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++;
        }
    }