求java写入txt解决中文乱码的的写法,不生成C盘,或者电脑里某个硬盘(C,D,E,F,G。。)
只写入到项目工程下的某个文件夹下例如。。webRoot/test/testData.txt
求高手帮忙。。

解决方案 »

  1.   

    System.out.println(System.getProperty("user.dir"));//user.dir 指定了当前的路径 
      

  2.   

    public static void main(String[] args) {
    BufferedWriter out = null;
    try{
    String filename = System.getProperty("user.dir") + "/1.txt";
    out = new BufferedWriter(new FileWriter(filename, true));
    out.write("张");
    }catch (IOException e) {
    e.printStackTrace();
    // error processing code  
    }finally{
    if (out != null) {
    try {
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    试了一下这个可以,没有乱码
      

  3.   


    public String getWebParth() {
    String WebParth = this.getClass().getResource("/").getPath();
    int i = WebParth.indexOf("/WEB-INF/");
    WebParth = WebParth.substring(0, i);
    return WebParth;
    }
    GeneralJS js = new GeneralJS();
    String path = js.getWebParth() + "/datajslib/datajslib.js";FileOutputStream os = new FileOutputStream(filePath);
    String str="你好@中国";
    os.write(str.getBytes("utf-8"));
    这个为乱码OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
    path, true), "utf-8");
    BufferedWriter bw = new BufferedWriter(osw);
    for(int i=0;i<10;i++){
    System.out.println(new Date().toString());
    bw.write("你好@中国");
    }
    这个写不进去
      

  4.   


    public static void main(String[] args) {
    BufferedWriter out = null;
    try{
    String filename = System.getProperty("user.dir") + "/1.txt";
    out = new BufferedWriter(new FileWriter(filename, true));
    out.write("张");
    }catch (IOException e) {
    e.printStackTrace();
    // error processing code  
    }finally{
    if (out != null) {
    try {
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
      

  5.   

    已经解决
    问题如下:file.mkdir(); 生成的文件在myeclipse下的编码格式为   ISO18030,改下生成后的文件编码就可以没乱码了
      

  6.   


    String realPath = "D:/resource/"; //路径
    File saveFile = new File(realPath + "文件名称");
    if (!saveFile.getParentFile().exists()) {
    saveFile.getParentFile().mkdirs();
    }
    FileOutputStream out = new FileOutputStream(saveFile, true);
    out.write(("内容").getBytes("UTF-8"));

    out.close();