程序解答,望各位帮忙,急!急!急!(在线等) 意思是用java编写一程序,功能如下:对原文件里的内容进行扫描,根据所扫描的内容,将其转换为java语句。如:扫描到”输出“时,就将其转换为System.out.printn("");并写入另一文件。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 faint这个,首先你得定义一套协议,比如“输出”字串就是替换为System.out.println("");,而其后面的一个字串则放入""里面你还得保证所读取的字串所有的可能性啊当然如果文件里面只有几种字串就简单多了另外,关于读、写文件,搜索论坛吧,n多关于这方面的东西 原来是这样呀!你先的定义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(); } }} 求解参数传递于引用的问题 菜鸟求指教 如何关联两个类,内存映象又作何解? java压缩文件可以设置解压密码吗? 写文件乱码问题!望各位哥姐能帮忙!谢谢! java程序每日重启问题? 为什么安装了 JBuilder 后,JDK 不能用了? 请教:一个全局对象,new了好多次,是不是导致内存一直增加? 初学者 请教一个迭代器的问题 我不知道这个问题是不是该在这问:关于JC! 给点建议!
这个,首先你得定义一套协议,比如“输出”字串就是替换为System.out.println("");,而其后面的一个字串则放入""里面
你还得保证所读取的字串所有的可能性啊
当然如果文件里面只有几种字串就简单多了
另外,关于读、写文件,搜索论坛吧,n多关于这方面的东西
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();
}
}
}