java - UNIX系统和Windows系统中读取.CSV文件的差异

标签 java windows unix file-io csv

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

上面的场景在 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 计算机传输到 UNIX 计算机时,即使 .csv 文件是文本文件,在读取 .csv 文件时也会存在差异。存在隐藏的空格字符,它们在 UNIX 机器上的表示方式可能有所不同。

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

一个建议是使用 dos2unix 命令。

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

希望有帮助。

关于java - UNIX系统和Windows系统中读取.CSV文件的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4081750/

相关文章:

java - org.openqa.selenium.InvalidElementStateException : invalid element state: SyntaxError

java - 如何使用谓词过滤列表

C++ CreateProcess 无法从 Windows 7 上的套接字接收路径 (64)

windows - 计划任务 : setting a working directory?

linux - sed 模式匹配直到第一次匹配

java - 如何使用 Dart 加密视频文件?

java - 通过JAAS : !角色登录报错是什么意思?

unix - 目录的递归列表

windows - 如何在用户不活动一段时间后锁定应用程序?

unix - 使用 Unix find(1),如何在一棵树中查找比另一棵树中的对应文件更新的文件?