2016-04-24 90 views
-5

我正在尝试使用Java编写文本编辑器,但似乎无法使“打开文件”功能正常工作。当我运行代码时,它只显示文件的第一行。我已经尝试了所有来自How to read a large text file line by line using Java?的代码片段,但它仍只读取第一行。为什么我不能读取java中所有文件的行?

这是我曾尝试:

  JMenuItem mntmOpen = new JMenuItem("Open"); 
      mntmOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); 
      mntmOpen.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

      if (e.getSource() == mntmOpen) { 
       int returnVal = fc.showOpenDialog(null); 

       if (returnVal == JFileChooser.APPROVE_OPTION) { 
        File file = fc.getSelectedFile(); 
        //This is where a real application would open the file. 

        Path HI = file.toPath(); 


        try(Stream<String> lines = Files.lines(HI) 
          ){ 
         for(String line : (Iterable<String>) lines::iterator) 
         { 
          editorPane.setText(line); 
         } 
        }catch (IOException e1) { 

          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
         } 


       } 
      } 
     } 
    }); 
+1

显示你的尝试。 –

+2

*“但它仍只读取第一行。”*您的代码不会这样做。请创建一个[mcve]。 – Tom

+0

也许你需要一个StringBuilder并把一些新的行放到你的输出中? –

回答

相关问题