FileWriter fw=new FileWriter(folderPath + "/'"+tempName+"'");FileWriter fw=new FileWriter(folderPath + "/"+tempName);
你是调试的时候走到这里就结束了,还是运行的时候?

解决方案 »

  1.   

    我认为:
    import java.io.*;
    public class FileTest{
    public static void main(String[] args){
    String tempName="c.txt"; File file = new File(".",tempName);
    try{
    //file.createNewFile(); String folderPath ="export"; FileWriter fw = new FileWriter(folderPath + "\\"+ tempName);
    }catch(Exception e){
    System.out.println("e");
    }
    }}我试验了一下,得出如下结论:
    1。如果你当前文件夹下存在export目录,那么new FileWriter时就会在该目录下,创建一个新的文件
    2。如果不存在export目录,那么就抛出异常。
    因此你应该保证当前目录下存在export目录,否则,你应该先创建该目录。
      

  2.   

    给你个实例,相信可以帮到你.
    package com.io;import java.io.File;
    import java.io.IOException;
    import java.io.FileWriter;
    import java.io.BufferedWriter;
    import java.util.Date;
    import java.text.SimpleDateFormat;/**
     * Created by IntelliJ IDEA.
     * User: heyj
     * Date: 2003/09/03
     * Time: 13:56:55
     * To change this template use Options | File Templates.
     */
    public class FileTest {    /**
         * create txt file by current date+time
         * @param String _fileName
         * @return boolean
         * @throws IOException
         */
        public boolean ctFile(String _path, String _fileName) throws IOException {
            boolean success = false;
            String fileName = _fileName;
            String filePath = _path;
            try {
                File file = new File(filePath);
                if (!file.isDirectory()) {
                    file.mkdirs();
                }
                File f = new File(filePath + File.separator + fileName + ".txt");
                success = f.createNewFile();        } catch (IOException e) {
                e.printStackTrace();
            }
            return success;    }    /**
         * write string into specify file
         * @param _fileName
         * @throws IOException
         */
        public void writeFile(String _path, String _fileName) throws IOException {        try {
                BufferedWriter out = new BufferedWriter(new FileWriter(_path + File.separator + _fileName + ".txt"));
                out.write("this is a test string!");
                out.newLine();
                out.flush();
                BufferedWriter appOut = new BufferedWriter(new FileWriter(_path + File.separator + _fileName + ".txt", true));            appOut.write("this string is append str");
                appOut.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }    }    public static void main(String args[]) {        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String fileName = formatter.format(new Date());        FileTest b = new FileTest();
            try {
                //create file
                boolean succ = b.ctFile("c:\\aa\\bb", fileName);
                System.out.println("create file success:" + succ);            //write file
                b.writeFile("c:\\aa\\bb", fileName);
                System.out.println("write file success!");        } catch (IOException e) {
                e.printStackTrace();
            }    }}
      

  3.   

    希望大家与我交流.msn:[email protected]