I/O2—File类

File类是java.io包中唯一代表磁盘文件本身的对象。File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中的一些方法,实现创建、删除、重命名等操作。File类的对象主要用来获取文件本身的一些信息,如文件所在的目录、文件长度、文件读写权限等。数据流可以将数据写入到文件中,文件也是数据流最常用的数据媒体。

文件的创建与删除

可以使用File类创建一个文件对象。通常使用以下3中方法来创建文件对象:
(1)File(String pathname)

该构造方法通过将给定的路径名字符串转换为抽象路径名来创建一个新的File实例。

语法如下:
New File(String pathname)

其中,pathname指路径名称(包括文件名)。

例如:
File file=New File("C:/Test/test.txt");

表示在C盘的Test文件夹下创建了一个test.txt文件

(2)File(String parent,String child)

该构造方法根据定义的父路径和子路径字符串(包含文件名)创建一个新的File对象。

语法如下:

new File(String parent,String child)

parent:父路径字符串。例如:C:/或C:/Test

child子路径字符串。例如:test.txt

(3)File(File file,String child)

该构造方法根据parent抽象路径名和child路径名字符串来创建一个新的File实例对象。

语法如下:

new File(File file,String child)

file:父路径对象,例如:C:/Test/

child:子路径字符串,例如:test.txt


当使用File类创建一个文件时,例如:

File file=new File("C:/Test/1.txt");

如果当前目录("C:/Test/")中已存在1.txt,可以通过文件对象的dele()方法将其删除,如果不存在1.txt文件,File类对象可通过createNewFile()方法创建一个名称为1.txt的文件。

实例:
package FileIO;
import java.io.*;//导入I/O包
public class FileTest1 {//创建FileTest1类
public static void main(String[] args) {
File file=new File("test.txt");//创建文件对象,位置已默认在当前程序所在文件目录下
if(file.exists()){//判断文件是否存在
file.delete();//文件存在则删除
System.out.println("test.txt文件已删除!");
}
else{//文件不存在
try{
file.createNewFile();//创建文件
System.out.println("test.txt文件创建成功!");
System.out.println(file.getAbsolutePath());//查看新文件的绝对路径
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}

获取文件信息

File类提供了许多的获取文件信息的方法

想了解细节请戳——File类方法详细信息

实例2:

package FileIO;
import java.io.File;//导入
public class FileTest2 {

public static void main(String[] args) {
File file=new File("test.txt");//创建文件对象,位置已默认在当前程序所在文件目录下
if(file.exists()){//判断文件是否存在
String filename=new String();//获取文件名称
filename=file.getName();
long leng=file.length();//获取文件长度
boolean B=file.isFile();//查看文件是否存在
long time=file.lastModified();//查看文件最后一次修改时间

file.delete();//文件存在则删除
System.out.println("文件名称是:"+filename);
System.out.println("文件的长度为:"+leng);
System.out.println("w文件是否存在:"+B);
System.out.println("文件最后一次被修改的时间:"+time);
System.out.println("test.txt文件已删除!");
}
else{//文件不存在
try{
file.createNewFile();//创建文件
System.out.println("test.txt文件创建成功!");
System.out.println(file.getAbsolutePath());//查看新文件的绝对路径
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}

stellaechen@STELLAECHEN-MC1 Python-2.7.13 % make ./python.exe -E -S -m sysconfig --generate-posix-vars ;\ if test $? -ne 0 ; then \ echo "generate-posix-vars failed" ; \ rm -f ./pybuilddir.txt ; \ exit 1 ; \ fi running build running build_ext building '_Qt' extension gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/stellaechen/download/Python-2.7.13/Mac/Include -I. -IInclude -I./Include -I/usr/local/include -I/Users/stellaechen/download/Python-2.7.13/Include -I/Users/stellaechen/download/Python-2.7.13 -c /Users/stellaechen/download/Python-2.7.13/Mac/Modules/qt/_Qtmodule.c -o build/temp.macosx-15.4-arm64-2.7/Users/stellaechen/download/Python-2.7.13/Mac/Modules/qt/_Qtmodule.o -Wno-deprecated-declarations error: unable to open output file 'build/temp.macosx-15.4-arm64-2.7/Users/stellaechen/download/Python-2.7.13/Mac/Modules/qt/_Qtmodule.o': 'Operation not permitted' 1 error generated. building '_tkinter' extension gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tcl.framework/Versions/Current/PrivateHeaders -I/System/Library/Frameworks/Tk.framework/Headers -I/System/Library/Frameworks/Tk.framework/Versions/Current/PrivateHeaders -I/usr/X11R6/include -I/Users/stellaechen/download/Python-2.7.13/Mac/Include -I. -IInclude -I./Include -I/usr/local/include -I/Users/stellaechen/download/Python-2.7.13/Include -I/Users/stellaechen/download/Python-2.7.13 -c /Users/stellaechen/download/Python-2.7.13/Modules/_tkinter.c -o build/temp.macosx-15.4-arm64-2.7/Users/stellaechen/download/Python-2.7.13/Modules/_tkinter.o -framework Tk clang: warning: -framework Tk: 'linker' input unused [-Wunused-command-line-argument] In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_tkinter.c:71: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/tk.h:86:11: fatal error: 'X11/Xlib.h' file not found 86 | # include <X11/Xlib.h> | ^~~~~~~~~~~~ 1 error generated. building '_ctypes' extension gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I_ctypes/darwin -I/Users/stellaechen/download/Python-2.7.13/Mac/Include -I/Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include -I/Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/powerpc -I. -IInclude -I./Include -I/usr/local/include -I/Users/stellaechen/download/Python-2.7.13/Include -I/Users/stellaechen/download/Python-2.7.13 -c /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c -o build/temp.macosx-15.4-arm64-2.7/Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.o -DMACOSX In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:113: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:65:3: error: "Unsupported MacOS X CPU type" 65 | # error "Unsupported MacOS X CPU type" | ^ In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:113: In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:73: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffitarget.h:12:2: error: "Unsupported CPU type" 12 | #error "Unsupported CPU type" | ^ In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:113: In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:74: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/fficonfig.h:51:2: error: "Unknown CPU type" 51 | #error "Unknown CPU type" | ^ In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:113: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:171:5: error: unknown type name 'ffi_abi' 171 | ffi_abi abi; | ^ /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:193:2: error: unknown type name 'ffi_sarg' 193 | ffi_sarg sint; | ^ /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:194:2: error: unknown type name 'ffi_arg' 194 | ffi_arg uint; | ^ /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:307:11: error: unknown type name 'ffi_abi' 307 | ffi_abi abi, | ^ In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:129: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/ctypes.h:99:5: error: unknown type name 'ffi_closure' 99 | ffi_closure *pcl_write; /* the C callable, writeable */ | ^ 8 errors generated. Python build finished, but the necessary bits to build these modules were not found: _bsddb _curses _curses_panel _sqlite3 _ssl bsddb185 bz2 dbm dl gdbm imageop linuxaudiodev nis ossaudiodev readline spwd sunaudiodev zlib To find the necessary bits, look in setup.py in detect_modules() for the module's name. Failed to build these modules: _ctypes _Qt _tkinter running build_scripts stellaechen@STELLAECHEN-MC1 Python-2.7.13 % make install /usr/bin/install -c python.exe /usr/local/bin/python2.7 install: /usr/local/bin/INS@20xsco: Permission denied make: *** [altbininstall] Error 71 stellaechen@STELLAECHEN-MC1 Python-2.7.13 %
最新发布
07-05
FAILED: /home/zb/PointAugmenting/det3d/models/img_backbones/DCNv2/build/temp.linux-x86_64-3.8/home/zb/PointAugmenting/det3d/models/img_backbones/DCNv2/src/cpu/dcn_v2_cpu.o c++ -MMD -MF /home/zb/PointAugmenting/det3d/models/img_backbones/DCNv2/build/temp.linux-x86_64-3.8/home/zb/PointAugmenting/det3d/models/img_backbones/DCNv2/src/cpu/dcn_v2_cpu.o.d -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /home/zb/anaconda3/envs/mmdet3d/include -fPIC -O2 -isystem /home/zb/anaconda3/envs/mmdet3d/include -fPIC -DWITH_CUDA -I/home/zb/PointAugmenting/det3d/models/img_backbones/DCNv2/src -I/home/zb/anaconda3/envs/mmdet3d/lib/python3.8/site-packages/torch/include -I/home/zb/anaconda3/envs/mmdet3d/lib/python3.8/site-packages/torch/include/torch/csrc/api/include -I/home/zb/anaconda3/envs/mmdet3d/lib/python3.8/site-packages/torch/include/TH -I/home/zb/anaconda3/envs/mmdet3d/lib/python3.8/site-packages/torch/include/THC -I/usr/local/cuda/include -I/home/zb/anaconda3/envs/mmdet3d/include/python3.8 -c -c /home/zb/PointAugmenting/det3d/models/img_backbones/DCNv2/src/cpu/dcn_v2_cpu.cpp -o /home/zb/PointAugmenting/det3d/models/img_backbones/DCNv2/build/temp.linux-x86_64-3.8/home/zb/PointAugmenting/det3d/models/img_backbones/DCNv2/src/cpu/dcn_v2_cpu.o -DTORCH_API_INCLUDE_EXTENSION_H '-DPYBIND11_COMPILER_TYPE="_gcc"' '-DPYBIND11_STDLIB="_libstdcpp"' '-DPYBIND11_BUILD_ABI="_cxxabi1011"' -DTORCH_EXTENSION_NAME=_ext -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++14 In file included from /home/zb/anaconda3/envs/mmdet3d/lib/python3.8/site-packages/torch/include/ATen/Parallel.h:140:0, from /home/zb/anaconda3/envs/mmdet3d/lib/python3.8/site-packages/torch/include/TH/THTensorApply.h:4, from /home/zb/anaconda3/envs/mmdet3d/lib/python3.8/site-packages/torch/include/TH/THTensor.h:5, from /home/zb/anaconda3/envs/mmdet3d/lib/python3.8/site-packages/torch/include/TH/TH.h:13, from /home/zb/PointAugmenting/det3d/models/i
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值