//exampleA.xml<?xml version="1.0" encoding="GBK"?><bookList>    <book>        <name>Java编程入门</name>        <author>张三</author>        <publishDate>2002-6-6</publishDate>        <price>35.0</price>    </book>    <book>        <name>XML在Java中的应用</name>        <author>李四</author>        <publishDate>2002-9-16</publishDate>        <price>92.0</price>    </book></bookList>//testJDOM.javaimport org.jdom.*;import org.jdom.output.*;import org.jdom.input.*;import java.io.*;public class TestJDOM{    public static void main(String args[])throws Exception{                SAXBuilder sb = new SAXBuilder();        //从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了        Document doc = sb.build(new FileInputStream("exampleA.xml"));                //加入一条处理指令        ProcessingInstruction pi = new ProcessingInstruction            ("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");        doc.addContent(pi);        Element root = doc.getRootElement(); //得到根元素        java.util.List books = root.getChildren(); //得到根元素所有子元素的集合        Element book = (Element)books.get(0); //得到第一个book元素        //为第一本书添加一条属性        Attribute a = new Attribute("hot","true");          book.setAttribute(a);        Element author = book.getChild("author"); //得到指定的字元素        author.setText("王五"); //将作者改为王五        //或 Text t = new Text("王五");book.addContent(t);        Element price = book.getChild("price"); //得到指定的字元素        //修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势        author.setText(Float.toString(50.0f));                 String indent = "    ";        boolean newLines = true;        XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");        outp.output(doc, new FileOutputStream("exampleB.xml"));    }};

解决方案 »

  1.   

    各位老大,是在Java中直接可以读文本文件啊
      

  2.   

    http://java.sun.com/docs/books/tutorial/essential/attributes/properties.html// create and load default properties
    Properties defaultProps = new Properties();
    FileInputStream in = new FileInputStream("defaultProperties");
    defaultProps.load(in);
    in.close();// create program properties with default
    Properties applicationProps = new Properties(defaultProps);// now load properties from last invocation
    in = new FileInputStream("appProperties");
    applicationProps.load(in);
    in.close();
      

  3.   

    请问各位我的代码如下,不知道对不对,请各位多多指教,谢谢!
    String ss=null;
    FileReader fr=new FileReader();
    ss=FileReader.toString();引用的类是import import java.text.*; and import java.io.*;上面的运行老是出错,请各位多多指正,谢谢!
      

  4.   

    我的天啊, 你就是要读普通文件啊. 那你为什么要问"在java中如何读取配置文件"啊.另外, 你上面错的厉害. import java.io.*;
    import java.util.*;public class ReadFile {
       public static void main(String args[]) {
         if (args.length == 0) {
           System.err.println("Please provide filename to process");
           System.exit(-1);
         }
          String filename = args[0];
          File file = null;
          FileReader fr = null;
          BufferedReader br = null; 
          try {
            file = new File(filename);
            if (file.exists() && file.isFile()) {          fr = new FileReader(file);
              br = new BufferedReader(fr);
              String line;
              while ((line = br.readLine()) != null) {
                System.out.println(line);
              }
             
            }      } catch (IOException e) {
            System.err.println("Problem: " + e);
          } finally
          {
           if (br != null)
           {
           try
           {
                br.close();
              }
              catch(IOException e) {}  
           }
          }
       }
    }