首页 > 其他分享 >文件file操作

文件file操作

时间:2022-11-04 11:00:10浏览次数:39  
标签:文件 File System file println 操作 public out

文件创建方法一

import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;

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

}
@Test

public void create1(){
String filePath = "D:\\file1.txt";
File file = new File(filePath);

try{
file.createNewFile();

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

 

 文件创建方法二

import org.testng.annotations.Test;

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

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

}
@Test
public void create2(){
File parentFile = new File("D:\\");
String fileNane = "file2.txt";
File file = new File(parentFile,fileNane);

try{
file.createNewFile();
System.out.println("文件2创建成功");
}catch(IOException e){
throw new RuntimeException(e);
}
}

}

 

 文件创建方法三

import org.testng.annotations.Test;

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

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

}
@Test
public void create3(){
String parentPath = "d:\\";
String filePath = "file3.txt";

File file = new File(parentPath,filePath);

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

}

 

 

 

 

 获取文件信息的方法

import org.testng.annotations.Test;

import java.io.File;

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

}
//获取文件信息
@Test
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());
}

}

 

 文件删除

import org.testng.annotations.Test;

import java.awt.desktop.SystemEventListener;
import java.io.File;

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
}

 

 

 

 

import java.util.Scanner;

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");
}
}

 

 

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

 

文件复制

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class copyPic {
//file读和写实现复制文件
public static void main(String[] args) throws Exception {
//创建file对象
File f = new File("d:\\test.jpg");
//判断文件是否存在
if (f.exists()) {
System.out.println("text.jpg存在,可以复制");
} else {
f.createNewFile();
System.out.println("text.jpg不存在,新建成功,可以复制");
}
//创建FileInputStream对象
FileInputStream inp = new FileInputStream(f);
//创建FileOutputStream对象
//判断demo目录是否存在
File f1 = new File("d:\\demo");
if (f1.isDirectory()) {
FileOutputStream out = new FileOutputStream("e:\\demo\\" + f.getName());
byte bytes[] = new byte[1024];
int temp = 0;//边读边写
while ((temp = inp.read(bytes)) != -1) { //读
out.write(bytes, 0, temp); //写
}
//结束
inp.close();
out.close();
System.out.println("文件拷贝成功!");
} else {
//新键demo目录
f1.mkdir();
System.out.println("demo目录不存在,已经新建成功,继续复制");
FileOutputStream out = new FileOutputStream("d:\\demo\\" + f.getName());
byte bytes[] = new byte[1024];
int temp = 0;
while ((temp = inp.read(bytes)) != -1) { //读
out.write(bytes, 0, temp); //写
}
//结束
inp.close();
out.close();
System.out.println("文件拷贝成功!");
}
}
}

 

 

 



标签:文件,File,System,file,println,操作,public,out
From: https://www.cnblogs.com/anxiety123/p/16857009.html

相关文章

  • 【FAQ】调用华为云空间文件管理接口出现"errorCode":"21000403"
    ​ 1、问题描述调用华为云空间文件管理接口,总是返回错误,{"error":{"errorDetail":[{"domain":"global","reason":"authError","description":"AccessForbidden","error......
  • 如何获得一个MonoBehavior类所在cs文件的路径
    1)如何获得一个MonoBehavior类所在cs文件的路径​2)Addresssables是否可读取ScriptableObject类型的配置文件3)内置Shader中的部分宏是在哪里定义的4)Android升级到Target12部......
  • openresty lua时间操作
    在Lua中,函数time、date和difftime提供了所有的日期和时间功能。在OpenResty的世界里,不推荐使用这里的标准时间函数,因为这些函数通常会引发不止一个昂贵的系统调......
  • libmysqlclient.so.18: cannot open shared object file: No such file or directory
    DownloadTypeURLMirrorrepo.mysql.comBinaryPackage​​mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm​​SourcePackage​​mysql-community-5.7.26-1.el7.src.r......
  • 非文件流下载文件设置文件名
    前端下载如果是文件流的话,就直接使用downloadFile方法进行下载,如果是非文件流,要设置文件名,就要转成文件流的方式,解决通过download设置文件名的兼容问题,代码如下:download......
  • 图像基本操作
    数据读取-图像###图像读取img=cv2.imread('imag.jpg')###图像显示cv2.imshow('imag',img)###读取灰度图img=cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)#cv2.IM......
  • Core 获取配置文件
    publicclassTraceUploadService{//实例化配置节点privatereadonlyIConfigurationconfiguration;publicTraceUploadService(IConfigurationco......
  • prometheus 基于consul、file实现服务发现
    安装consul  consul官网:https://www.consul.io/  Consul是分布式k/v数据存储集群,目前常用于服务的服务注册和发现。   官网下载  下载链接1https://www.......
  • 文件上传的 2 种方式 - 上传到 腾讯云仓库
    1.前端上传  -第三方云仓库 https://cloud.tencent.com/login?s_url=https%3A%2F%2Fconsole.cloud.tencent.com%2F注册使用腾讯云步骤;  需要实名认证;  ......
  • makefile-make的工作方式
    GNU的make工作时的执行步骤入下:(想来其它的make也是类似)1、读入所有的Makefile。2、读入被include的其它Makefile。3、初始化文件中的变量。4、推导隐晦规则......