2016-06-30 12 views
0

私は、テキストファイルからデータを読み込んでJListに読み込み、いくつかの問題に取り組んでいます。私はテストのためにこれを使用していますテキストファイルを読み込んでJListにインポートしますか?

String stringpath = "C:/StackOverflowQuestion.txt"; 
    Path path = Paths.get(stringpath, args); 

    List<String> readText = Files.readAllLines(path); 
    for (String elem : readText){ 
     System.out.println(elem); 
    } 

、と私は、ファイルの内容がさえコンソール出力ウィンドウに印刷するように見えることはできません。ここに私はこれまで持っているものです。私は何が欠けていますか?エラーは生成されません。

編集:ボタンの下で作業するようにしました。私のJFrameクラスのメインメソッドではどうしてうまくいかないでしょうか?

編集:投稿の完全なコード:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package Forms; 

import java.awt.Toolkit; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.nio.charset.Charset; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.List; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JOptionPane; 

/** 
* 
* 
*/ 
public class RemoteDesktop extends javax.swing.JFrame { 

    /** 
    * Creates new form Home 
    */ 
    public RemoteDesktop() { 
     initComponents(); 
     setLocationRelativeTo(null); 
     setIcon(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     backButton = new javax.swing.JButton(); 
     connectButton = new javax.swing.JButton(); 
     jTextField1 = new javax.swing.JTextField(); 
     jLabel3 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jLabel1 = new javax.swing.JLabel(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     resultList = new javax.swing.JList<>(); 
     jMenuBar1 = new javax.swing.JMenuBar(); 
     jMenu1 = new javax.swing.JMenu(); 
     jMenuItem1 = new javax.swing.JMenuItem(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setName("Home"); // NOI18N 
     setResizable(false); 
     setSize(new java.awt.Dimension(585, 360)); 

     backButton.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N 
     backButton.setText("Back"); 
     backButton.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       backButtonActionPerformed(evt); 
      } 
     }); 

     connectButton.setText("Connect"); 
     connectButton.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       connectButtonActionPerformed(evt); 
      } 
     }); 

     jLabel3.setText("Computer name or IP address:"); 

     jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Forms/Capture.PNG"))); // NOI18N 

     jLabel1.setText("Select a computer from the list below or type it in manually."); 

     jScrollPane1.setViewportView(resultList); 

     jMenu1.setText("File"); 

     jMenuItem1.setText("Exit"); 
     jMenuItem1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jMenuItem1ActionPerformed(evt); 
      } 
     }); 
     jMenu1.add(jMenuItem1); 

     jMenuBar1.add(jMenu1); 

     setJMenuBar(jMenuBar1); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(backButton) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 129, Short.MAX_VALUE) 
         .addComponent(jLabel1)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) 
         .addGap(18, 18, 18) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
           .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING) 
           .addComponent(connectButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
           .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
           .addComponent(jLabel3) 
           .addGap(41, 41, 41))))) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(backButton) 
        .addComponent(jLabel1)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jLabel3) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(17, 17, 17) 
         .addComponent(connectButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addContainerGap(18, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     Home homeFrame = new Home(); 

     homeFrame.setVisible(true); 
     this.setVisible(false); 

    }           

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     this.dispose(); 
    }           

    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {            
     // TODO add your handling code here: 
     try { 
      File file = new File("P:/IT/Shared Knowledge/FILE SHARE/computers.txt"); 
      FileReader fileReader = new FileReader(file); 
      BufferedReader bufferedReader = new BufferedReader(fileReader); 
      StringBuffer stringBuffer = new StringBuffer(); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       stringBuffer.append(line); 
       stringBuffer.append("\n"); 
      } 
      fileReader.close(); 
      System.out.println("Contents of file:"); 
      System.out.println(stringBuffer.toString()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    }            
    private void setIcon() { 
     setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("SelfService.png"))); 
    } 

    /** 
    * @param args the command line arguments 
    * @throws java.io.IOException 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(RemoteDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(RemoteDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(RemoteDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(RemoteDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 
     //</editor-fold> 
     //</editor-fold> 
     //</editor-fold> 
     //</editor-fold> 
     //</editor-fold> 
     //</editor-fold> 
     //</editor-fold> 





     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new RemoteDesktop().setVisible(true); 

      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton backButton; 
    private javax.swing.JButton connectButton; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JMenu jMenu1; 
    private javax.swing.JMenuBar jMenuBar1; 
    private javax.swing.JMenuItem jMenuItem1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JList<String> resultList; 
    // End of variables declaration     
} 
+0

「args」とは何ですか? –

+0

間違いはありますか? fnfまたは類似のものと同様? –

+0

エラーはまったくありません。私はこれを行ういくつかの方法を試したが、それはファイルからデータを読み込む10以上の方法があり、どのファイルを選択するかはわからないと思われる。 –

答えて

0

このファイルを形成読んで、それをプリントアウトする必要があります。これは上のコードのmodです。作業ディレクトリの別のファイルパスを使用する必要があるかもしれません。

String stringpath = "C:/StackOverflowQuestion.txt"; 
    ArrayList<String> readText = new ArrayList<String>(); 
    try(BufferedReader br = new BufferedReader(new FileReader(stringpath))) 
    { 
     String temp =br.readLine(); 
     while(temp == null) 
     { 
      readText.add(temp); 
      temp =br.readLine(); 
     } 


    }catch(Exception e) 
    { 
     System.out.println(e.toString()); 
    } 
    for(int i =0;i<readText.size();i++) 
    { 
     System.out.println(readText.get(i)); 
    } 
関連する問題