创建文件

来源:互联网 发布:淘宝店铺出租合同 编辑:程序博客网 时间:2024/05/16 05:19

创建文件

一、创建文件

1、创建文件的两种方式:

(1)利用FileOutputStream的构造方法,创建一个文件

(2)利用File对象的方法:createNewFile()创建一个文件

2、创建文件并读写的实例

(1)FileOutputStream的构造方法创建一个文件

package testtomcat;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import org.junit.Test;/* * 利用FileOutputStream的构造方法,创建一个文件 * 1、注意路径的写法,只要不是  xxx\xxx\xxx的写法【此种路径写法会报错】, *     其余方式:(1)xxx/xxx/xxx(2)xxx//xxx//xxx(3)xxx\\xxx\\xxx,都是可以正确的创建文件的。 * *2、读写方法一  ~~~  读写方法四 *File f1 = new File("d://temp1//temp1");// 创建目录 *f1.mkdirs(); *File f11 = new File(f1, "temp.txt");// 利用:FileOutputStream,创建了temp.txt文件 *OutputStream out = new FileOutputStream(f11);//写文件成功 * * 3、读写方法五 *   File f1 = new File("d://temp1//temp1.txt"); *    然后f1.mkdirs();f1.createNewFile(); *    创建结果是:创建了都是目录:temp1:一级目录和temp1.txt二级目录,没有文件文件的创建 *    最终:写文件,利用:FileOutputStream:OutputStream out0 = new FileOutputStream(f1) *                 抛出异常::ava.io.FileNotFoundException: D:\temp2\temp.txt (拒绝访问。) *                 因为:temp.txt不是文件,而是目录,不能进行写操作,所以拒绝访问 *  */public class CreateFile {// 读写方法一  成功@Testpublic void test1() throws IOException {// 创建文件一:成功File f1 = new File("d://temp1//temp1");// 创建目录if (!f1.exists()) {f1.mkdirs();}File f11 = new File(f1, "temp.txt");// 创建文件byte[] buf = new byte[1024];int len = 0;OutputStream out = new FileOutputStream(f11);// 方式1:成功读取InputStream in = MyServlet.class.getClassLoader().getResourceAsStream("1.txt");while ((len = in.read(buf)) != -1) {out.write(buf, 0, len);}System.out.println("成功1");in.close();out.close();}// 读写方法二  成功@Testpublic void test2() throws IOException {// 创建文件二:成功File f2 = new File("D:/temp2/temp2");// 创建目录if (!f2.exists()) {f2.mkdirs();}File f21 = new File(f2, "temp.txt");// 创建文件InputStream in2 = MyServlet.class.getClassLoader().getResourceAsStream("1.txt");byte[] buf = new byte[1024];int len = 0;OutputStream out2 = new FileOutputStream(f21);// //方式2:成功读取while ((len = in2.read(buf)) != -1) {out2.write(buf, 0, len);}System.out.println("成功2");in2.close();out2.close();}// 读写方法三 失败@Testpublic void test3() throws IOException {// 创建文件三:失败File f3 = new File("D:\temp3\temp3");// 创建目录 // 异常:java.io.FileNotFoundException: D: emp3 emp3\temp.txt(文件名、目录名或卷标语法不正确。)if (!f3.exists()) {f3.mkdirs();}File f31 = new File(f3, "temp.txt");// 创建文件 失败InputStream in3 = MyServlet.class.getClassLoader().getResourceAsStream("1.txt");byte[] buf = new byte[1024];int len = 0;OutputStream out3 = new FileOutputStream(f31);// 方式三:失败while ((len = in3.read(buf)) != -1) {out3.write(buf, 0, len);}System.out.println("成功3");in3.close();out3.close();}// 读写方法四  成功@Testpublic void test4() throws IOException {// 创建文件四:成功File f4 = new File("D:\\temp4\\temp4");// 创建目录if (!f4.exists()) {f4.mkdirs();}File f41 = new File(f4, "temp.txt");// 创建文件InputStream in4 = MyServlet.class.getClassLoader().getResourceAsStream("1.txt");byte[] buf = new byte[1024];int len = 0;OutputStream out4 = new FileOutputStream(f41);// //方式4:成功读取while ((len = in4.read(buf)) != -1) {out4.write(buf, 0, len);}System.out.println("成功4");in4.close();out4.close();}// 读写方法五   失败@Testpublic void test0() throws IOException {File f0 = new File("D:\\temp2\\temp.txt");// 此种方式,最后通过FileOutputStream创建的都是目录//异常:java.io.FileNotFoundException: D:\temp2\temp.txt (系统找不到指定的路径。)//原因:分析,当前d盘下的temp2目录不存在//为了解决这个异常,所以尝试写了下面的4行代码:添加创建目录,创建文件if (!f0.exists()) {f0.mkdirs();//创建目录f0.createNewFile();//创建文件   失败}//返现,添加了上面四行代码后,仍旧抛出了下面所示的异常//异常:ava.io.FileNotFoundException: D:\temp2\temp.txt (拒绝访问。)//我们可以知道,我们创建了temp2和temp.txt的目录,而我们的temp.txt是文件,创建失败了,//因为temp.txt已经先创建成了目录,而不是文件byte[] buf = new byte[1024];int len = 0;InputStream in0 = MyServlet.class.getClassLoader().getResourceAsStream("1.txt");OutputStream out0 = new FileOutputStream(f0);// 方式1:成功读取while ((len = in0.read(buf)) != -1) {out0.write(buf, 0, len);}System.out.println("成功0");in0.close();out0.close();}}

(2)createNewFile()方式创建一个文件            

备注:注意路径的写法,只要不是  xxx\xxx\xxx的写法【此种路径写法会报错】,其余方式:(1xxx/xxx/xxx2xxx//xxx//xxx3xxx\\xxx\\xxx,都是可以正确的创建文件的。

package testtomcat;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;/* * 利用File对象的方法:createNewFile()创建一个文件 */public class CreateFile2 {public static void main(String[] args) {try {String path = "e:\\test\\test\\test3.txt";File file = new File(path);if (!file.exists()) {file.getParentFile().mkdirs();//创建除 最后一个文件名以外的所有父目录/*File f2=file.getParentFile();返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。 抽象路径名的父 路径名由路径名的前缀(如果有),以及路径名名称序列中最后一个名称以外的所有名称组成。如果名称序列为空,那么该路径名没有指定父目录。 即:抽象路径名的父 路径名 就是:除最后一个文件名以外的所有父目录eg:"e:\\test\\test\\test3.txt"中的  "e:\\test\\test"f2.mkdirs();//创建目录:  test的1级目录,text的2级目录System.out.println(f2.getAbsolutePath());//结果:e:\test\test*/}//  "e:\\test\\test\\test3.txt"  中的  text3.txt文件创建file.createNewFile();//创建文件// writeFileWriter fw = new FileWriter(file, true);BufferedWriter bw = new BufferedWriter(fw);bw.write("xxxaffdf");bw.flush();bw.close();fw.close();// readFileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);String str = br.readLine();System.out.println(str);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

原创粉丝点击