意思是用java编写一程序,功能如下:对原文件里的内容进行扫描,根据所扫描的内容,将其转换为java语句。如:
扫描到”输出“时,就将其转换为System.out.printn("");并写入另一文件。

解决方案 »

  1.   

    faint
    这个,首先你得定义一套协议,比如“输出”字串就是替换为System.out.println("");,而其后面的一个字串则放入""里面
    你还得保证所读取的字串所有的可能性啊
    当然如果文件里面只有几种字串就简单多了
    另外,关于读、写文件,搜索论坛吧,n多关于这方面的东西
      

  2.   

    原来是这样呀!你先的定义import.java.io.*;或者其他的定义,准备为你的读,写做准备,然后书写本身的内容!下面是我写的关于读写XML的一个简单的类子,自己看看吧!1能不能帮帮你!
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import java.io.*;
    public class xmldisplay {
    public static void  main(String args[]){
      try{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=factory.newDocumentBuilder();
        Document doc=builder.parse(new File("d:/20025241111.xml"));
        NodeList links =doc.getElementsByTagName("link");
        for (int i=0;i<links.getLength();i++){
          Element link=(Element) links.item(i);
          System.out.print("Content: ");
          System.out.println(link.getElementsByTagName("text").item(0).getFirstChild().getNodeValue());
          System.out.print("URL: ");
          System.out.println(link.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());
          System.out.print("Author: ");
          System.out.println(link.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());
          System.out.print("Date: ");
          Element linkdate=(Element) link.getElementsByTagName("date").item(0);
          String day=linkdate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
          String month=linkdate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
          String year=linkdate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
          System.out.println(day+"-"+month+"-"+year);
          System.out.print("Description: ");
          System.out.println(link.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
          System.out.println();
        }
      }catch(Exception e){
        e.printStackTrace();
      }}
    }
    package xmlwriter;
    import javax.xml.parsers.*;
    /*import javax.xml.transform.*;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;*/
    import org.w3c.dom.*;
    import java.io.*;
    import java.lang.*;
    import com.sun.xml.tree.XmlDocument;
    import java.util.*;
    public class xmlwriter {public static void  main(String args[]){
    try{
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder=factory.newDocumentBuilder();
      Document doc=builder.parse(new File("d:/link.xml"));
         //---取得变量----
      String text="Wudong's Homepage";
      String url="www.wudong.com";
      String author="Wudong Liu";
      String discription="A site from Wudong Liu, give u lots of suprise!!!";  //-------------
      //get system time
      GregorianCalendar now=new GregorianCalendar();
      int Year=now.get(now.YEAR);
      int Month=now.get(now.MONTH)+1;
      int Date=now.get(now.DATE);
      int Hour=now.get(now.HOUR_OF_DAY);
      int Minute=now.get(now.MINUTE);
      int Second=now.get(now.SECOND);
      //
      Text textseg;
      Element link=doc.createElement("link");  Element linktext=doc.createElement("text");
      textseg=doc.createTextNode(text);
      linktext.appendChild(textseg);
      link.appendChild(linktext);  Element linkurl=doc.createElement("url");
      textseg=doc.createTextNode(url);
      linkurl.appendChild(textseg);
      link.appendChild(linkurl);  Element linkauthor=doc.createElement("author");
      textseg=doc.createTextNode(author);
      linkauthor.appendChild(textseg);
      link.appendChild(linkauthor);  java.util.Calendar rightNow = java.util.Calendar.getInstance();
      String day=Integer.toString(rightNow.get(java.util.Calendar.DAY_OF_MONTH));
      String month=Integer.toString(rightNow.get(java.util.Calendar.MONTH)+1);
      String year=Integer.toString(rightNow.get(java.util.Calendar.YEAR));
      Element linkdate=doc.createElement("date");  Element linkdateday=doc.createElement("day");
      textseg=doc.createTextNode(day);
      linkdateday.appendChild(textseg);  Element linkdatemonth=doc.createElement("month");
      textseg=doc.createTextNode(month);
      linkdatemonth.appendChild(textseg);
      Element linkdateyear=doc.createElement("year");
      textseg=doc.createTextNode(year);
      linkdateyear.appendChild(textseg);  linkdate.appendChild(linkdateday);
      linkdate.appendChild(linkdatemonth);
      linkdate.appendChild(linkdateyear);
      link.appendChild(linkdate);  Element linkdiscription=doc.createElement("description");
      textseg=doc.createTextNode(discription);
      linkdiscription.appendChild(textseg);
      link.appendChild(linkdiscription);  doc.getDocumentElement().appendChild(link);  ((XmlDocument)doc).write(new FileOutputStream(new File("d:/"+Year+Month+
        Date+Hour+Minute+".xml")));  /*TransformerFactory tFactory =TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer();
      DOMSource source = new DOMSource(doc);
      StreamResult result = new StreamResult(new java.io.File("links.xml"));
      transformer.transform(source, result);*/
             }catch(Exception e){
        e.printStackTrace();
      }
      }
    }