首页 > 其他分享 >文件的建立于查询

文件的建立于查询

时间:2022-11-06 00:11:43浏览次数:51  
标签:文件 File 建立 System 查询 file println public out

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class IO {
public static void main(String[] args) {

}

// 方式一
@Test
public void createFile() {
String filePath = "d:\\file.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println(file.getName());
} catch (IOException e) {
e.printStackTrace();
}
}

// 方式二
@Test
public void createFile1() {
File parentFile = new File("D:\\");
String fileName = "file2.txt";
File file1 = new File(file, s);

try {
file1.createNewFile();
System.out.println("done!");
} catch (IOException e) {
e.printStackTrace();
}
}

// 方式三
@Test
public void createFile2() {
String parentPath = "d:\\";
String fileName = "file3.txt";
File file = new File(parentPath, filePath);

try {
file.createNewFile();
System.out.println("文件3创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}


// 文件信息
@Test
public void info() {
/**
* 创建文件
*/
File file = new File("D:\\data\\file.txt");

try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 调用方法得到文件信息
*/
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());


}


// 文件夹(目录)操作
@Test
public void createDir1() {
String dirName = "d:\\data\\test";
File directory = new File(dirName);
try {
boolean hasSucceeded = directory.mkdir();
System.out.println("创建文件夹结果(不含父文件夹):" + hasSucceeded);
} catch (Exception e) {
e.printStackTrace();
}

}

@Test
public void createDir2() {
String dirName = "d:\\data1\\test";
File directory = new File(dirName);
try {
boolean hasSucceeded = directory.mkdirs();
System.out.println("创建文件夹结果(包含父文件夹):" + hasSucceeded);
} catch (Exception e) {
e.printStackTrace();
}

}

@Test
public void deleteFile() {
File file = new File("D:\\data\\text.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (file.delete()) {
System.out.println("empty");

} else {
System.out.println(file.getName());
}
}


// 基本键盘输入
public class scanPrintTest {
public static void main(String[] args) {
//创建Scanner对象,接受从控制台输入
Scanner input = new Scanner(System.in);
//接受String类型
String str = input.next();
//输出结果
System.out.println(str);
System.out.println("hello world");

}
}

// 常见键盘输入类型
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);
//int类型的数据
System.out.print("请输入一个int类型的数:");
int i = input.nextInt();
System.out.println();
//字符串类型的数据
System.out.print("请输入-个string类型的数:");
String s = input.next();
System.out.println(s);
}
}

}

 

标签:文件,File,建立,System,查询,file,println,public,out
From: https://www.cnblogs.com/mikasaakerman/p/16861764.html

相关文章

  • 文件的相关操作
    一、文件的概念文件是具有符号名的、在逻辑上具备完整意义的一组具备相关性的信息项的有序序列,可用于存储数据。其中信息项是构成文件内容的基本单位。除此之外。文件......
  • 盘点一个Pandas新手在文件读取路上遇到的问题
    大家好,我是皮皮。一、前言国庆期间在Python铂金交流群【暮雨和】问了一个Pandas处理的问题,提问截图如下:代码截图如下:新手上路,遇到的问题还是挺多的。二、实现过程......
  • shell-文件查找命令笔记三
    文件查找-find命令格式:find[路径][选项][操作]选项-name根据文件名查找-iname根据文件名查找,忽略大小写-perm根据文件权限查找find/etc-perm777-prun......
  • Openssl Des3对压缩文件进行加密命令详解
    群友提问:致力于明天:tar-cvf-WMG_Back_"$Today"|openssldes3-salt-khY91gd3GJAAfghECleLwWQAPGK9Cxs-out$dir_backup_today.tar.des3致力于明天:有人懂这个......
  • 34、Java——一个案例学会Dom4j解析技术对XML文件的增删改查
    ✅作者简介:热爱国学的Java后端开发者,修心和技术同步精进。案例使用Dom4j解析技术实现对animal.xml文件进行增删改查操作。链接:​​dom4j包下载​​​将dom4j的jar包导入......
  • 001.项目初始化,生成逆向文件
    1.整合Mybatis1.1在pom.xml中添加文件<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-......
  • java 文件类
    java常用的文件操作1~文件的创建(三种不同方法):(1)根据路径构建一个File对象:newFile(Stringpathname)(2)根据父目录文件+子路径构建:newFile(Fileparent,Stringchild)(3)......
  • Qt获取文件存储路径,绝对路径与相对路径,斜杠与反斜杠转换
     Qt获取文件存储路径QStringdirPath=QFileDialog::getExistingDirectory(this,tr("浏览选择文件夹"),tr("C:"));//返回用户选择的路径if(dirPath.isEmpty())//......
  • 更新:删除指定文件夹下所有重复文件
    #!/usr/bin/python3.9#2022-11-05还可优化代码有冗余importosimporthashlibimporttimeimportsysdefchoiceWhat(): """ #选择功能 #1手动输入路径......
  • 文件
    1.什么是文件?文件是我们保存数据的地方。2.文件流文件在程序中以流的形式来操作的。流:数据在数据源(文件)和程序(内存)的路径输入流:文件到内存的路径输出流:内存到文件的......