简体   繁体   English

如何在JFileChooser中保存csv文件?

[英]How to save a csv file in JFileChooser ?

Hi I am not able to save a csv file.I am not even getting error in the write command.I have used FileChooser. 嗨我无法保存csv文件。我甚至没有在write命令中出错。我使用过FileChooser。 And can any one explain the use of Approve option as i am new to swing. 任何人都可以解释使用Approve选项,因为我是新手。

if(e.getActionCommand()=="SaveTweet")
         {
        JFileChooser savePlaylistDialog = new JFileChooser();
        savePlaylistDialog.setSelectedFile(new File( ".csv"));
        int status = savePlaylistDialog.showSaveDialog(savePlaylistDialog);

        try {   
            if (status == JFileChooser.APPROVE_OPTION) {    
                File savePlaylist = savePlaylistDialog.getSelectedFile();                                            

        ICsvListWriter csvWriter = null;
         try { 
              TwitterProject obj=new TwitterProject();
             String [][] tweet=obj.generateBottweet(username,interval,Btweet,no);  

               csvWriter = new CsvListWriter(new FileWriter("savePlaylist"), 

           CsvPreference.STANDARD_PREFERENCE);

               for (int i = 0; i < tweet.length; i++) {
               csvWriter.write(tweet[i]);
             }

      } catch (IOException e1) {
                e1.printStackTrace(); 
      } finally {
         try {
         System.out.println("closing file");
         csvWriter.close();
                } catch (IOException e1) {
             }
      }

      } else if (status == JFileChooser.CANCEL_OPTION) {
                // User has pressed cancel button
  }
 } catch (Exception ex) {
          JOptionPane.showMessageDialog(null, "File could not be written, try again.");
        }
    }
 }

Two things jump out... 跳出来的两件事......

First, you comparing Object reference of String instead of contents... 首先,比较String Object引用而不是内容...

if(e.getActionCommand()=="SaveTweet")

Will very rarely be true, as the Object references will most likely never be equal, instead, you should try using something like... 很少会是真的,因为对象引用很可能永远不会相等,相反,你应该尝试使用类似的东西......

if("SaveTweet".equals(e.getActionCommand()))

Which will compare the contents of the two String s, but will also guard against possible NullPointerException s... 这将比较两个String的内容,但也会防止可能的NullPointerException ...

Second, you are ignoring the File the the user selected, for example, you get a reference to the file the user selects using... 其次,您忽略了用户选择的File ,例如,您获得了用户使用的文件的引用...

File savePlaylist = savePlaylistDialog.getSelectedFile();

But then you create a FileWriter which simply uses a String value... 但是你创建了一个只使用String值的FileWriter ......

csvWriter = new CsvListWriter(new FileWriter("savePlaylist"));

Instead, should try using something like... 相反,应该尝试使用像......

csvWriter = new CsvListWriter(new FileWriter(savePlaylist));

Which will write the contents to the file that the user selects... 哪个会将内容写入用户选择的文件...

ps- I would also drop... ps-我也会放弃......

savePlaylistDialog.setSelectedFile(new File( ".csv"));

As it doesn't actually make sense, I doubt there would be file called .csv ...or more to the point, it's highly unlikely... 因为它实际上没有意义,我怀疑会有一个名为.csv ...或更多的文件,这是非常不可能的......

To compare String objects in java use .equals() method instead of "==" operator 要比较java中的String对象,请使用.equals()方法而不是“==”运算符

change 更改

if(e.getActionCommand()=="SaveTweet")

to

if("SaveTweet".equals(e.getActionCommand()))

if you want to ignore the case use like below code 如果你想忽略这种情况,请使用如下代码

if("SaveTweet".equalsIgnoreCase(e.getActionCommand()))

Did you also test the writing method? 你也测试了写作方法吗? Which implementation of csvwriter do you use? 您使用哪种csvwriter实现? In some cases (com.csvwriter) you need to call flush() before close(). 在某些情况下(com.csvwriter),您需要在close()之前调用flush()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM