创建文件的三种方式:
1.package yjtest.demo;
import java.io.File;
import java.io.IOException;
public class testcreate {
public static void main(String[] args) throws IOException {
File file = new File("D:\\test");
file.createNewFile();
}
}
2.package yjtest.demo;
import javafx.scene.Parent;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
public class testcreate2 {
public static void main(String[] args) {
File parentFile = new File("D:\\");
String fileName = "file2.txt";
File file = new File(parentFile,fileName);
try{
file.createNewFile();
System.out.println("文件二创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.package yjtest.demo;
import java.io.File;
import java.io.IOException;
public class testcreate3 {
public static void main(String[] args) {
String parentPath = "D:\\";
String filePath = "file3.txt";
File file = new File(parentPath,filePath);
try{
file.createNewFile();
System.out.println("文件三创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
文件信息的查询:
package yjtest.demo;
import java.io.File;
public class testcheck {
public static void main(String[] args) {
File file = new File("D:\\file1.txt");
System.out.println("文件名称:"+file.getName());
System.out.println("文件绝对路径:"+file.getAbsolutePath());
System.out.println("文件父目录:"+file.getParent());
System.out.println("文件大小(字节):"+file.length());
System.out.println("文件是否存在:"+file.exists());
System.out.println("是否是文件:"+file.isFile());
System.out.println("是否是目录:"+file.isDirectory());
}
}
4.文件夹的创建:
package yjtest.demo;
import java.io.File;
import java.io.IOException;
public class testcreate {
public static void main(String[] args) throws IOException {
File file = new File("D:\\test");
file.mkdir();
}
}
5.Scanner与Ptintln:
1.package yjtest.demo;
import java.util.Scanner;
public class testjavaio {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str = input.next();
System.out.println(str);
System.out.println("hello world");
}
}
2.package yjtest.demo;标签:file,System,学习,Javaio,File,println,public,out From: https://www.cnblogs.com/20215220509yj/p/16851249.html
import java.util.Scanner;
public class testkeyin {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个double类型的数:");
double d = input.nextDouble();
System.out.println(d);
System.out.println("请输入一个int类型的数");
int i = input.nextInt();
System.out.println(i);
System.out.println("请输入一个string类型的数");
String s = input.next();
System.out.println(s);
}
}