越简单越好,最好能运行。请教高手。

解决方案 »

  1.   

    import java.io.File;
    import java.io.IOException;public class Test {
        public static void main(String[] args) throws IOException {
            File file = new File("a.txt");
            if (!file.exists()) {
                boolean b = file.createNewFile();
                System.out.println(b ? "文件创建成功" : "文件创建失败");
            }
        }
    }
      

  2.   

    自己看看
    http://topic.csdn.net/u/20110108/09/4f9315a2-84cd-4f40-9a5e-645d9bb251d6.htmlimport java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    public class TXT {
        public static void main(String[] args){
            List names=new ArrayList();
            names.add("name1");
            names.add("name2");
            names.add("name3");
            names.add("name4");
            names.add("name5");
            File f = new File("E:\\123.txt");
             if(f.exists()){
                    System.out.print("文件存在");
                   }else{
                    System.out.print("文件不存在");
                    try {
                        f.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }//不存在则创建
                   }        try {
                BufferedReader input = new BufferedReader(new FileReader(f));
                BufferedWriter output = new BufferedWriter(new FileWriter(f));
                for(int i=0;i<names.size();i++)
                    output.write((String)names.get(i)+"\r\n");
                
                               output.close();        } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            
            
        }}
      

  3.   


    import java.io.File;
    import java.io.IOException;/**
     * 新建文件实例代码
     */
    public class CreateTxtFile {
    public static void main(String[] args) throws IOException {
    File file = new File("c:/newTxtFile.txt");
    //父目录如果不存在,那么先新建父目录
    if(!file.getParentFile().exists()){
    file.getParentFile().mkdirs();
    }
    //判断文件是否存在,不存在新建
    if(!file.exists()){
    file.createNewFile();
    }
    }
    }
      

  4.   

    import java.io.File;
    public class Test
    {
    public static void main(String[] args)
    {
    try{
    File file = new File("a.txt"); 
         file.createNewFile();
       }catch(Exception e)
       {
       e.printStackTrace();
       } }
    }
      

  5.   


    //创建一个文件对象
    File file=new File("d:\\output.text");
      

  6.   

    new 完 File 还要create一下。
      

  7.   

    import java.io.File; 
    import java.io.IOException; public class Test { 
        public static void main(String[] args) throws IOException { 
            File file = new File("a.txt"); 
            if (!file.exists()) { 
                boolean b = file.createNewFile(); 
                System.out.println(b ? "文件创建成功" : "文件创建失败"); 
            } 
        } 
    }
      

  8.   


    #include <stdio.h>
    #include <string.h>int main()
    {
        FILE txt = open("c:/1.txt","rb+");
        if(txt == NULL)
        {
            printf("Create 1.txt fail");
            return -1;
        }       
    }