Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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 输出在Mac上正确,但在Windows上不正确_Java_Eclipse - Fatal编程技术网

Java 输出在Mac上正确,但在Windows上不正确

Java 输出在Mac上正确,但在Windows上不正确,java,eclipse,Java,Eclipse,我们有一个Java程序,它从.xml文件中提取信息,将其转换为字符串,然后将字符串写入.txt文件。然后将该文件附加到另一个.txt文件,该文件将保存来自多个.xml文件的所有输入。在Mac上的Eclipse中,程序会将所有.xml代码正确地附加到最终的.txt文件中 但是,当在Eclipse中的Windows计算机上运行相同的Java程序时,最终的.txt文件并不像在Mac上那样收集所有输入。在Windows和Mac上读/写文件是否存在已知差异?特别是当最初在Mac上编写的代码被传输到Wind

我们有一个Java程序,它从.xml文件中提取信息,将其转换为字符串,然后将字符串写入.txt文件。然后将该文件附加到另一个.txt文件,该文件将保存来自多个.xml文件的所有输入。在Mac上的Eclipse中,程序会将所有.xml代码正确地附加到最终的.txt文件中

但是,当在Eclipse中的Windows计算机上运行相同的Java程序时,最终的.txt文件并不像在Mac上那样收集所有输入。在Windows和Mac上读/写文件是否存在已知差异?特别是当最初在Mac上编写的代码被传输到Windows计算机时

这是我们的代码:

package edu.uci.ics.jung.samples;

import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.util.Scanner;

public class ReadXMLFile1 {

    public static void main(String argv[]) throws IOException { //throws ioexception allows the filewriter code below (specifically writing the "t# b" title)
          //while loop to grab all xml files in the form of name0, name1, name2, etc and perform txt file conversion
         int numberofgraphs=10; //indicate number of graphs importing/reading/appending
         int x=0;
         int b = 0;
          while (x < numberofgraphs){
                FileWriter pagetowrite = new FileWriter("graphtotal1.txt", true); //true says to append string "scannedstuff" to graphtotal.txt (graphtotal.txt can be replaced by any file in visualization folder, you name it!)
                BufferedWriter now = new BufferedWriter(pagetowrite); //more advanced file writer, included in order to use newLine() method below
                now.write("t# "+b); //writes the title t# "b" at top of each graph set
                now.newLine(); //returns to next line of file (helps keep format of original .txt)
                now.close(); //ends file writing for that line of text file



              try {

                  String xmlname = ("Untitled"+b+".xml");

                  File fXmlFile = new File(xmlname);
                  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                  Document doc = dBuilder.parse(fXmlFile);
                  doc.getDocumentElement().normalize();


                  NodeList oList = doc.getElementsByTagName("node"); //writing to txt file the node info
                  for (int temp = 0; temp < oList.getLength(); temp++) {

                       Node oNode = oList.item(temp);
                       if (oNode.getNodeType() == Node.ELEMENT_NODE) {

                          Element fElement = (Element) oNode;

                          String nodename = getTagValue("name", fElement);
                          char nodenumber = nodename.charAt( 1 );
                          String nodelabel = getTagValue("string", fElement);

                          FileWriter pagetowriteon = new FileWriter("newinput1.txt", true); //true says to append string "scannedstuff" to newinput1.txt (newinput1.txt can be replaced by any file in visualization folder, you name it!)
                        BufferedWriter out = new BufferedWriter(pagetowriteon); //more advanced file writer, included in order to use newLine() method below
                        out.write("v"+ " "); //writes the letter v to indicate a node
                        out.write(nodenumber+" "); //writes number of node created
                        out.write(nodelabel); // writes the name of node..its label
                        out.newLine(); //returns to next line of file (helps keep format of original .txt)
                        out.close(); //ends file writing for that line of text file

                       }
                    }
                  NodeList nList = doc.getElementsByTagName("arc");//writing to txt file the arc info


                  for (int temp = 0; temp < nList.getLength(); temp++) {

                   Node nNode = nList.item(temp);
                   if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                      Element eElement = (Element) nNode;


                      //String arcname = getTagValue("name", eElement);
                      String arcnode1 = getTagValue("From", eElement);
                      char node1 = arcnode1.charAt( 1 );
                      String arcnode2 = getTagValue("To", eElement);
                      char node2 = arcnode2.charAt( 1 );
                      String arclabel = getTagValue("string", eElement);

                      FileWriter pagetowriteon = new FileWriter("newinput1.txt", true); //true says to append string "scannedstuff" to newinput1.txt (newinput1.txt can be replaced by any file in visualization folder, you name it!)
                    BufferedWriter out = new BufferedWriter(pagetowriteon); //more advanced file writer, included in order to use newLine() method below
                    out.write("u"+ " "); //writes the letter u to indicate an edge
                    out.write(node1+" ");
                    out.write(node2+ " ");
                    out.write(arclabel);
                    out.newLine(); //returns to next line of file (helps keep format of original .txt)
                    out.close(); //ends file writing for that line of text file

                   }
                }
                //an option (mostly for individual graph use with simplegraphview...probably unecessary since simplegraph not used until after Data mining) to write to a newly named textfile so can run simplegraph view without having to change file name
                  File file = new File("newinput1.txt");

                //File (or directory) with new name (makes it easy to change one file name here instead of two in code above)
                  File file2 = new File("newinput.txt");
                  file.renameTo(file2);

                //this strategy of appending-> keep-> allows you to change one file name for giant append file..Filewriter line below ("graphtotal.txt")

                //start of appending text file
                 // boolean graphready;
              //assign if(boolean){ = to true when ready to append
                  if (true){
                    File file3 = new File ("newinput.txt"); //indicate which file
                    Scanner scan = new Scanner (file3); //read the text file indicated above
                            while (scan.hasNextLine()){ //will loop until no more lines of code detected; this loop reads and appends text to desired file
                                String scannedstuff3 = scan.nextLine(); //converts next line of code to a string
                                FileWriter pagetowriteon = new FileWriter("graphtotal.txt", true); //true says to append string "scannedstuff" to graphtotal.txt (graphtotal.txt can be replaced by any file in visualization folder, you name it!)
                                BufferedWriter out = new BufferedWriter(pagetowriteon); //more advanced file writer, included in order to use newLine() method below
                                //out.write("t# "+b); //writes the title t3 0 at top of each graph set
                                //out.newLine(); //returns to next line of file (helps keep format of original .txt)
                                out.write(scannedstuff3); //writes scanned line of code to new file indicated in FileWriter("desiredfile.txt", true)
                                out.newLine(); //returns to next line of file (helps keep format of original .txt)
                                out.close(); //ends file writing for that line of text file

                            }
                            scan.close(); //ends scanning of txt file indicated
                }

              } catch (Exception e) {
                e.printStackTrace();
              }
              x=x+1;
              b=b+1;


          }
    }






  private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
  }

}
包edu.uci.ics.jung.samples;
导入java.io.FileWriter;
导入java.io.BufferedWriter;
导入java.io.IOException;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.DocumentBuilder;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
导入org.w3c.dom.Node;
导入org.w3c.dom.Element;
导入java.io.File;
导入java.util.Scanner;
公共类ReadXMLFile1{
publicstaticvoidmain(字符串argv[])抛出IOException{//throws IOException允许下面的filewriter代码(特别是编写“t#b”标题)
//while循环以name0、name1、name2等形式获取所有xml文件,并执行txt文件转换
int numberofgraphs=10;//表示导入/读取/追加的图形数
int x=0;
int b=0;
while(x