javaIO流

文件的创建:

package io.stream.file;
import java.io.File;
import java.io.IOException;
public class 文件创建 {
public static void main(String[] args) {
//File file = new File(pathname);使用第一种构造方法创建一个文件
try {
File file = new File("D:\\IOStream\\text1.txt");
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//File file = new File(parent:StringPath,child:path);

使用第二种构造方法创建一个文件
try {
File file = new File("D:\\IOStream\\","text2.txt");
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//File file = new File(parent:filePath,child:path);

使用第三种构造方法创建一个文件
try {
File filepath = new File("D:\\IOStream\\");
if (filepath.exists()) {
filepath.mkdirs(); 
}
File file = new File(filepath,"text3.txt");
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

2.获取相关文件信息

public void Info(){
//先创建文件对象
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());
}
}
3.目录的操作
import org.testng.annotations.Test;
import java.io.*;
public class fileDirectory {
public static void main(String[] args) {
}
@Test
//删除文件public void fileDelete(){
String filePath = "D:\\file1.txt";
File file = new File(filePath);
if(file.exists()){
if(file.delete()){
System.out.println(filePath+"删除成功");
}else {
System.out.println(filePath+"删除失败");
};
}else{
System.out.println("文件不存在");
}
}
@Test
public void fileDeleteD(){
String filePath = "D:\\file1.txt";
File file = new File(filePath);
if(file.exists()){if(file.delete()){
System.out.println(filePath+"删除成功");
}else {
System.out.println(filePath+"删除失败");
};
}else{
System.out.println("目录不存在");
}
}
//判断目录是否存在,不存在就创建
@Test
public void fileDeleteD1(){
String dirPath = "D:\\test\\dir1.txt";
File file = new File(dirPath);
if(file.exists()){
System.out.println(dirPath+"该目录已经存在");
}else{
if(file.mkdirs()){
System.out.println("创建成功");
}else {System.out.println("创建失败");
};
}
}
//InputStream
//OutputStream
//Writer
//Reader
}
4、Scanner 与 Println
1、基本键盘输入
import org.testng.annotations.Test;
import java.util.Scanner;public class scanPrintTest {
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、常见键盘输入类型
import java.util.Scanner;
public class scanTest {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);//double 类型的数据
System.out.print("请输入一个 double 类型的数:");
double d = input.nextDouble();
System.out.println(d);
System.out.print("请输入一个 int 类型的数:");
int i = input.nextInt();
System.out.println(i);
//字符串类型的数据
System.out.print("请输入一个 string 类型的数:");
String s = input.next();
System.out.println(s);
}
}
posted @   RiceZK  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示