createfile.java

来自「主要是对于JAVA的编程的基本语言 希望能够帮得上你。」· Java 代码 · 共 139 行

JAVA
139
字号
package exercise;

import java.io.*;

public class CreateFile {

	public static void main(String[] args) {
		//Method1: File(String pathname) 
		File f1=new File("f://Java_Example//exercise//sun.txt");
		try {
			f1.createNewFile();					
		} catch (IOException e) {			
			e.printStackTrace();
		}		
		
		//Method2: File(File parent, String child) 
		File parent=new File("f://");
		File f2=new File(parent,"Java_Example//exercise//moon.txt");
		try {
			f2.createNewFile();						
		} catch (IOException e) {			
			e.printStackTrace();
		}	
		
		//Method3: File(String parent, String child) 
		File f3=new File("c://","star.txt");
		try {
			f3.createNewFile();
		} catch (IOException e) {			
			e.printStackTrace();
		}
		
				
		//如何在当前盘的目录下创建文件
		File curDrive=new File(File.separator);  //获取了当前盘
		File f4=new File(curDrive,"Java_Example"+File.separator+"exercise"+File.separator+"flower.txt");
		try {
			f4.createNewFile();	
		} catch (IOException e) {			
			e.printStackTrace();
		}
		
		//删除文件
		f1.delete();
		
		//退出时才删除
		f2.deleteOnExit();
		
		long length1=f1.length();
		System.out.println(length1);
		long length2=f2.length();
		System.out.println(length2);
		
		System.out.println(f1.getAbsolutePath()+", "+f1.getName());		
		System.out.println(f2.getAbsolutePath()+", "+f2.getName());
		System.out.println(f3.getAbsolutePath()+", "+f3.getName());
		System.out.println(f4.getAbsolutePath()+", "+f4.getName());
		
		//写文件
		//(1)字节流 FileOutputStream
		
		try {
			FileOutputStream fos=new FileOutputStream(f3);			
			fos.write(97);
			fos.write('\n');
			byte[] place={'l','u','z','h','o','u'};
			fos.write(place);
			byte[] name={65,108,105,99,101};
			fos.write('\n');
			fos.write(name);
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		} catch (IOException e) {			
			e.printStackTrace();
		}			
		
		//(2)字符流 FileWriter
		
		try {
			FileWriter fr=new FileWriter(f4);
			fr.write(65);			
			fr.write(10);
			fr.write("Today is a nice day.");
			char[] name={'M','i','k','e'};
			fr.write(name);			
			fr.flush();
			
		} catch (IOException e) {			
			e.printStackTrace();
		}
		
		//读文件
		//(1)字节流 FileInputStream
		System.out.println("star.txt的内容:");
		try {
			FileInputStream fis=new FileInputStream(f3);			
			//方法一
			int i;
			while((i=fis.read())!=-1){
				System.out.print((char)i);				
			}
			System.out.println("\n=========");
			//方法二			
			fis=new FileInputStream(f3);
			for(int j=0;j<f3.length();j++){
				System.out.print((char)fis.read());
			}
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		} catch (IOException e) {			
			e.printStackTrace();
		}		
		//(2)字符流 FileReader/BufferedReader
		System.out.println("\nflower.txt的内容:");
		try {
			FileReader fr=new FileReader(f4);
			//按字符读取
			int k;
			while((k=fr.read())!=-1){
				System.out.print((char)k);
			}		
			System.out.println("\n========");
			//按行读取
			fr=new FileReader(f4);
			BufferedReader br=new BufferedReader(fr);
			String nextLine="";
			while((nextLine=br.readLine())!=null){
				System.out.println(nextLine);
			}
			
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		} catch (IOException e) {			
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?