java写文件,java写文件设置编码格式

发布时间:2023-01-08

本文目录一览:

  1. JAVA如何写XML文件?
  2. Java读写文件的几种方法
  3. java文件读写,在一个已经有内容的文件中,追加第一行,如何做到?
  4. java配置文件怎么写?
  5. java如何写入文件
  6. java文件读写

JAVA如何写XML文件?

import java.io.*;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class DOM4JTest {
    public static void main(String[] args) {
        Document doc = DocumentHelper.createDocument();
        doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl' href='students.xsl'");
        Element root = doc.addElement("students");
        Element eltStu1 = root.addElement("student").addAttribute("sn", "01");
        Element eltName1 = eltStu1.addElement("name");
        Element eltAge1 = eltStu1.addElement("age");
        eltName1.setText("张三");
        eltAge1.setText("20");
        Element eltStu2 = root.addElement("student").addAttribute("sn", "02");
        Element eltName2 = eltStu2.addElement("name");
        Element eltAge2 = eltStu2.addElement("age");
        eltName2.setText("李四");
        eltAge2.setText("18");
        try {
            OutputFormat format = new OutputFormat("\n", true);
            format.setEncoding("gb2312");
            // 可以把System.out改为你要的流。
            XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);
            xmlWriter.write(doc);
            xmlWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Java读写文件的几种方法

java读取配置文件的几种方法如下: 方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。 方式二:采用ResourceBundle类读取配置信息, 优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。缺点:只能加载类classes下面的资源文件且只能读取.properties文件。

java文件读写,在一个已经有内容的文件中,追加第一行,如何做到?

我的想法是可以用RandomAccessFile,这个类的seek方法,想在文件的哪个位置插入内容都行。所以你的第一行就不在话下了。但是,这个会覆盖你文件中插入位置后面的内容。相当于我们在输入的时候,按了键盘的insert键盘。所以,像你这种情况只能用临时文件来存储原有的内容,然后把要插入的数据写入文件,再把临时文件的内容追加到文件中。

void insert(String filename, int pos, String insertContent) { // pos是插入的位置
    File tmp = File.createTempFile("tmp", null);
    tmp.deleteOnExit();
    try {
        RandomAccessFile raf = new RandomAccessFile(filename, "rw");
        FileOutputStream tmpOut = new FileOutputStream(tmp);
        FileInputStream tmpIn = new FileInputStream(tmp);
        raf.seek(pos); // 首先的话是0
        byte[] buf = new byte[64];
        int hasRead = 0;
        while ((hasRead = raf.read(buf)) > 0) {
            // 把原有内容读入临时文件
            tmpOut.write(buf, 0, hasRead);
        }
        raf.seek(pos);
        raf.write(insertContent.getBytes());
        // 追加临时文件的内容
        while ((hasRead = tmpIn.read(buf)) > 0) {
            raf.write(buf, 0, hasRead);
        }
    }
}

java配置文件怎么写?

参考java.util.Properties对象进行书写,另外可以在网上找一些辅助书写材料。 代码:

public static void main(String[] args) {
    Properties p = new Properties();
    p.setProperty("id", "user1");
    p.setProperty("password", "123456");
    try {
        PrintStream stm = new PrintStream(new File("e:\\test.properties"));
        p.list(stm);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

java如何写入文件

package filewriter;
import java.io.FileWriter;
import java.io.IOException;
public class IOExceptionDemo {
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    public static void main(String[] args) {
        FileWriter fw = null;
        try {
            fw = new FileWriter("k:\\Demo.txt", true);
            fw.write("hello" + LINE_SEPARATOR + "world!");
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            if (fw != null)
                try {
                    fw.close();
                } catch (IOException e) {
                    throw new RuntimeException("关闭失败!");
                }
        }
    }
}

java文件读写

从你函数的签名来看,推测你做的是文件复制操作。 这样复制文件,在缓存分配和计算方面,都存在风险,你可能没有正确计算这些值或者strbuffer超出范围。可以在读写源文件的同时写入到新文件,例如:

private static void copyFileUsingFileStreams(File source, File dest) throws IOException {
    InputStream input = null;
    OutputStream output = null;
    try {
        input = new FileInputStream(source);
        output = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = input.read(buf)) > 0) {
            output.write(buf, 0, bytesRead);
        }
    } finally {
        input.close();
        output.close();
    }
}

另外还有3种复制文件方法,可以参见: 4 Ways to Copy File in Java