【问题标题】:Difference in Reading .CSV file in UNIX System & Windows System在 UNIX 系统和 Windows 系统中读取 .CSV 文件的区别
【发布时间】:2010-11-02 20:20:08
【问题描述】:

我创建了一个 JSP 代码,我们可以在其中上传 .csv 文件。 JSP 代码由读取 .csv 文件并将文件中的 url 与 DB 进行比较的 java 代码支持,如果 url 不存在,则将其添加到 DB 中。

上面的场景在windows系统中执行时绝对可以正常工作。

我将成功执行的 web 应用程序文件夹上传到 unix 系统。当我在 UNIX 系统中执行程序时,该工具不会将 URL 与 DB 进行比较并添加它。

我怀疑在 UNIX 系统中读取 .csv 文件时应该有一些问题。

我正在使用 fedora(linux) 操作系统。请让我知道在 windows 系统和 unix 系统之间读取 .csv 文件是否有任何差异。

我使用的.csv文件有以下内容,

http://www.topix.com,sdfasdf
http://rss.news.yahoo.com/rss/topstories,Apple
http://www.apple.com/354,sdfasdf
http://www.topix.com/rss/city/emporia-ks,sdfasdf
http://www.topix.com/rss/,sdfasdf
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml,sdfasdf
http://www.topix.com/rss/city/emp,sdfasdf
http://www.topix.com/rss/city/sandy-ut,dfgsdfg
http://www.apple.com,Yahoo

杰夫更新

try {
                    List items = uploadHandler.parseRequest(request);
                    Iterator itr = items.iterator();
                while(itr.hasNext()) {
                    FileItem item = (FileItem) itr.next();
                    if(item.isFormField()) {
                        out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
                    } else {
                        File file = new File(destinationDir,item.getName());
                        item.write(file);
                    //String temp=item.getName();
                        String fileToBeRead = "C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/Readcsv/files/"+item.getName();
                        String urlcnt="";
                        String srccnt="";
                        String contentType="";
                        Connection con=null;
                        Statement stmt=null;
                        final String rssvar="Rss";
                        final String other="Other";

                        int i=0;
                        int j=0;

                        try {
                            BufferedReader br = new BufferedReader(new FileReader(fileToBeRead));
                            String strLine = "";
                            StringTokenizer st = null;
                            while( (strLine = br.readLine()) != null)
                            {
                                st = new StringTokenizer(strLine, ",");
                                while(st.hasMoreTokens()){
                                    urlcnt=st.nextToken();
                                    srccnt=st.nextToken();
                                }
                                if(con==null){
                                    SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
                                        con=SQLConnection.getNewConnection();
                                        stmt=con.createStatement();
                                }
                                try{
                                    ResultSet rs;
                                    boolean hasRows=false;
                                    rs=stmt.executeQuery("select url from urls_linkins where url='"+urlcnt+"'");
                                while(rs.next()){
                                    hasRows=true;
                                    i++;
                                    }
                                if(!hasRows){
                                    j++;
                                    URL url = new URL(urlcnt);
                                    URLConnection url1=url.openConnection();
                                    contentType=url1.getContentType();
                                    PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_linkins(url, source_name, is_active, is_periodic, Link_Type, New_Entry) VALUES(?, ?, ?, ?, ?, ?)");
                                if(contentType.contains("rss") || contentType.contains("xml"))
                                {
                                    insertUrlStatement.setString(1, urlcnt);
                                    insertUrlStatement.setString(2, srccnt);
                                    insertUrlStatement.setInt(3, 1);
                                    insertUrlStatement.setInt(4, 0);
                                    insertUrlStatement.setString(5, rssvar);
                                    insertUrlStatement.setInt(6, 1);
                                    insertUrlStatement.executeUpdate();
                                    insertUrlStatement.close();
                                }
                                else{
                                    insertUrlStatement.setString(1, urlcnt);
                                    insertUrlStatement.setString(2, srccnt);
                                    insertUrlStatement.setInt(3, 1);
                                    insertUrlStatement.setInt(4, 0);
                                    insertUrlStatement.setString(5, other);
                                    insertUrlStatement.setInt(6, 1);
                                    insertUrlStatement.executeUpdate();
                                    insertUrlStatement.close();
                                    }
                                }
                                }
                                catch(Exception e){
                                    e.printStackTrace();
                                }
                            }

                            }catch(Exception e){
                                e.printStackTrace();
                            }finally{
                                out.println("<h2>"+j+" url has been added and "+i+" url already exists in the DB</h2>");
                                out.println("<a href=Addurl.jsp>Check URL</a>");
                                out.println("<a href=Addurl1.jsp>Add Single URL</a>");
                                out.println("<a href=uploadcsv.jsp>Add Multiple URL</a>");
                            }

                            }
                                out.close();
                            }
                            }catch(FileUploadException ex) {
                                log("Error encountered while parsing the request",ex);
                            } catch(Exception ex) {
                                log("Error encountered while uploading file",ex);
                        }

这是我对 .csv 文件的阅读代码。

【问题讨论】:

  • 有什么例外吗?其他消息?它究竟在哪里失败——例如,读取文件、连接到数据库、比较、更新数据库?
  • 没有例外..没有消息..连接到数据库并更新数据库工作正常。但问题在于读取文件
  • 听起来可能是行尾问题。您将无法正常工作的文件保存在哪里?
  • 你可以在你的问题中添加文件读取代码吗?几个想法:Windows (CRLF) 和 Unix (LF) 之间的权限或不同的换行符
  • @All 我在上面的代码中发现了一个错误,其中一行写着String fileToBeRead = "C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/Readcsv/files/"+item.getName();,但在 UNIX 系统中找不到提到的位置。

标签: java windows unix file-io csv


【解决方案1】:

是的,当您从 Windows 机器传输到 unix 机器时,即使它是文本文件,读取 .csv 文件也会有所不同。有隐藏的空格字符,在 unix 机器上可能会以不同的方式表示。

我怀疑它不比较 URL 的原因是因为空格字符可能是不同的 ASCII 值,所以它认为它们不同并将 URL 添加到数据库中。

一个建议是使用 dos2unix 命令。

http://kb.iu.edu/data/acux.html

希望对你有帮助。

【讨论】:

  • 它没有向数据库中添加任何内容。即使它没有比较。
  • > 这就是我当我尝试在 FEDORA OS 中将文件转换为 UNIX 格式时得到
猜你喜欢
  • 2011-05-02
  • 2013-07-31
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 2021-07-06
  • 1970-01-01
  • 2020-04-08
  • 2019-02-28
相关资源
最近更新 更多