JAVA IO - File 常量属性

import java.io.File;
import java.io.IOException;

public class FileConstants {
	public static void main(String args[]) {
		System.out.println(File.separator);
		System.out.println(File.separatorChar);
		System.out.println(File.pathSeparator);
		System.out.println(File.pathSeparatorChar);

		try {
			String filename = "testing.txt";
			String finalfile = "";
			String workingDir = System.getProperty("user.dir");

			finalfile = workingDir + File.separator + filename;

			System.out.println("Final filepath : " + finalfile);
			File file = new File(finalfile);

			if (file.createNewFile()) {
				System.out.println("Done");
			} else {
				System.out.println("File already exists!");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			 
	    	  String filename = "testing.txt";
	    	  String finalfile = "";
	    	  String workingDir = System.getProperty("user.dir");
	 
	    	  String your_os = System.getProperty("os.name").toLowerCase();
	    	  if(your_os.indexOf("win") >= 0){
	    		  finalfile = workingDir + "\\" + filename;
	    	  }else if(your_os.indexOf( "nix") >=0 || your_os.indexOf( "nux") >=0){
	    		  finalfile = workingDir + "/" + filename;
	    	  }else{
	    		  finalfile = workingDir + "{others}" + filename;
	    	  }
	 
	    	  System.out.println("Final filepath : " + finalfile);
	    	  File file = new File(finalfile);
	 
		  if (file.createNewFile()){
		     System.out.println("Done");
		  }else{
		     System.out.println("File already exists!");
		  }
	 
	    	} catch (IOException e) {
		     e.printStackTrace();
		}
	}
}



你可能感兴趣的:(java,IO,File)