Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays_Csv_Io - Fatal编程技术网

Java 对编程来说相当陌生,读取和写入二进制文件有困难。

Java 对编程来说相当陌生,读取和写入二进制文件有困难。,java,arrays,csv,io,Java,Arrays,Csv,Io,所以我有一个作业,我应该读一个.csv文件,然后把它写在一个单独的二进制文件中。csv文件提供了有关棒球运动员的奇怪格式的统计数据 以下是程序实际运行的GUI代码: import java.awt.*; import javax.swing.*; import java.util.*; import java.io.*; public class Homework3 extends JFrame { private JButton jbChoose = new JButton("Choo

所以我有一个作业,我应该读一个.csv文件,然后把它写在一个单独的二进制文件中。csv文件提供了有关棒球运动员的奇怪格式的统计数据

以下是程序实际运行的GUI代码:

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

public class Homework3 extends JFrame
{
   private JButton jbChoose = new JButton("Choose File to Convert");
   private JTextArea jtaDisc = new JTextArea("Welcome to the .csv to binary file converter. Simply click the button below and choose the .csv file you would like to write to binary.", 5, 20);
   private File fobj = null;

   public static void main(String[] args)
   {
      new Homework3();
   }

   public Homework3()
   {
      this.setSize(400, 175);
      this.setTitle("File Converter");
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      this.setResizable(false);

      jtaDisc.setWrapStyleWord(true);
      jtaDisc.setLineWrap(true);
      jtaDisc.setEditable(false);

      JPanel jpSouth = new JPanel();
         jpSouth.add(jbChoose);
         jpSouth.setAlignmentX(jbChoose.CENTER_ALIGNMENT);
      this.add(jpSouth, BorderLayout.SOUTH);

      JPanel jpNorth = new JPanel();
         jpNorth.add(jtaDisc);
      this.add(jpNorth, BorderLayout.NORTH);

      Homework3Button hw3b = new Homework3Button(this);
         jbChoose.addActionListener(hw3b);

      setVisible(true);
   }
   public File getFobj()
   {
      return fobj;
   }

   public void setFobj(File _fobj)
   {
      fobj = _fobj;
   }
}
这是我实际尝试阅读和写作的课程。我已经将它设置为打印出csv文件的值,这样我就可以看到哪里出错了。出于某种原因,它是读第一行,跳过第二行和第三行,而不是像我想的那样跳过第一行和第二行。它也没有达到足够的程度,无法在write方法中实际创建文件

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.util.*;

public class Homework3Button implements ActionListener 
{
   private Homework3 window;
   private ArrayList<Player> data = new ArrayList<Player>();
   private int pos; 

   public Homework3Button(Homework3 _window)
   {
      this.window = _window;
   }

   public void actionPerformed(ActionEvent ae)
   {
      doChoose();
      doRead();
      doWrite();
   }

   public void doChoose()
   {
      JFileChooser chooser = new JFileChooser(new File("."));
      FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel Documents", "csv");
      chooser.setFileFilter(filter);

      int returnVal = chooser.showDialog(window, "Select");
      if(returnVal == JFileChooser.APPROVE_OPTION)
      {
         File fobj = chooser.getSelectedFile();
         window.setFobj(fobj);
      } 
   }

   public void doRead()
   {
      File fobj = window.getFobj();
      if(fobj == null)
      {
         JOptionPane.showMessageDialog(null, "There was a problem reading your file.", "Opening Error", JOptionPane.ERROR_MESSAGE);
         return;
      }
      BufferedReader br = null;
      try
      {
          br= new BufferedReader(new FileReader(fobj));
      }
      catch(IOException ioe)
      {
         JOptionPane.showMessageDialog(null, "Cannot open file: " + ioe.getMessage(), "Cannot Open", JOptionPane.ERROR_MESSAGE);
         return;
      }
      data = new ArrayList<Player>();
      try
      {
         String line = br.readLine() ;
         while(line != null)
         {
            System.out.println(line);
            for(int i = 0; i <= 1; i++)
            {
                line = br.readLine();
            }
            Player pl = new Player(line);
            data.add(pl);
            line = br.readLine();
         }
      }
      catch(EOFException eofe)
      {
         System.out.println("Exception for ArrayList.");
      } 
      catch(IOException ioe)
      {
         JOptionPane.showMessageDialog(null, "Error reading file: " + ioe.getMessage(), "Bad Read", JOptionPane.ERROR_MESSAGE);
         return;
      }
      try
      {
          br.close();
      }
      catch(IOException ioe)
      {
         JOptionPane.showMessageDialog(null, "Cannot close file: " + ioe.getMessage(), "Cannot Close", JOptionPane.ERROR_MESSAGE);
         return;
      }
   }

   public void doWrite()
   {
      File fobj2 = new File("BaseballNames1.bin");

      if(fobj2 == null)
      {
         JOptionPane.showMessageDialog(null, "There was a problem writing to your file.", "Problem Writing", JOptionPane.ERROR_MESSAGE);
         return;
      }
      DataOutputStream dos = null;
      try
      {
         dos = new DataOutputStream(new FileOutputStream(fobj2));
      }
      catch(IOException ioe)
      {
         JOptionPane.showMessageDialog(null, "Cannot open file: " + ioe.getMessage(), "Cannot Open", JOptionPane.ERROR_MESSAGE);
         return;
      }
      System.out.println("Before For Loop");
      for(Player pl : data)
      {
         System.out.println("Inside For Loop");
         try
         {
            dos.writeUTF(pl.getName());
            dos.writeUTF(pl.getBirth());
            dos.writeInt(pl.getWeight());
            dos.writeUTF(pl.getHeight());
         }
         catch(IOException ioe)
         {
            JOptionPane.showMessageDialog(null, "Cannot write file: " + ioe.getMessage(), "Cannot Write", JOptionPane.ERROR_MESSAGE);
            return;
         }
         try 
         {
            dos.close();
         }
         catch(IOException ioe)
         {
            JOptionPane.showMessageDialog(null, "Cannot close file: " + ioe.getMessage(), "Cannot Close", JOptionPane.ERROR_MESSAGE);
            return;
         }
      }
   }    
}
以下是.csv文件格式,每行前面有一个空格:

namefirst , namelast  , birthday , birthmonth , birthyear , weight , height 

Starlin   , Castro    ,24,3,1990,190,72.8
Madison   , Bumgarner ,1,8,1989,215,76.0
Jason     , Heyward   ,9,8,1989,240,77.3
Ruben     , Tejada    ,27,10,1989,160,71.2
Jenrry    , Mejia     ,11,10,1989,160,72.5
Mike      , Stanton   ,8,11,1989,235,77.9
Dayan     , Viciedo   ,10,3,1989,240,71.1
Chris     , Sale      ,30,3,1989,170,77.2
Freddie   , Freeman   ,12,9,1989,225,77.7
Clayton   , Kershaw   ,19,3,1988,225,75.4
Travis    , Snider    ,2,2,1988,235,72.0
Elvis     , Andrus    ,26,8,1988,200,72.0
Trevor    , Cahill    ,1,3,1988,220,76.0
Rick      , Porcello  ,27,12,1988,200,77.0
Brett     , Anderson  ,1,2,1988,235,76.5
Fernando  , Martinez  ,10,10,1988,200,73.0
Jhoulys   , Chacin    ,7,1,1988,215,75.2
Chris     , Tillman   ,15,4,1988,200,77.8
Neftali   , Feliz     ,2,5,1988,215,75.5
Craig     , Kimbrel   ,28,5,1988,205,71.6

我知道这是很多问题,但我在网上看到的问题没有一个与我的具体问题相关,javadocs让我感到困惑。这是我使用Java IO的第一件大事,因此我非常感谢您的帮助。。。我不确定我在玩家类中声明数组的方式是否有问题,或者。。。非常感谢关于如何修复此混乱的任何建议。

您想跳过一行,但这将跳过两行:

for(int i = 0; i <= 1; i++)

欢迎来到堆栈溢出!我想这段代码的95%与您的问题无关。请创建一个示例来演示您的问题。我还没有看完您的所有代码,但有一个问题出现了—您的播放器构造函数正在做它不应该做的事情。构造器应该没有业务解析文本,但它应该做的只是创建一个新的播放器实例。解析属于I/O代码的其他地方,应该是可单独测试的,;是罪魁祸首,这意味着lineRead为空。检查调用播放器构造函数的位置。
for(int i = 0; i <= 1; i++)
String line = br.readLine(); // read first line
br.readLine(); // skip 2nd line
while((line = br.readLine()) != null) // read the rest till the end
{
    System.out.println(line);
    Player pl = new Player(line);
    data.add(pl);
}