Javaの基礎:13、Fileクラスの一般的に使用される方法はありますか?(コード例では)

一般的な方法のファイルは何ですか?

図1に示すように、共通の工法

(1)公開ファイル(文字列のパス名 ); // 絶対パス構造
(2)公開ファイル(文字列親 、文字列の子); // 相対パス構造
(3)公開ファイル(ファイルの親 、文字列の子); // もし最初に作成した親のインスタンスが、そこにあります。次いで、相対パス構成。


public class demo02File {
	public static void main(String[] args) {
		String fatherPath = "D:/other";
		String name = "1.jpg";
		
		// 相对路径构造
		File src1 = new File(fatherPath, name);
		File src2 = new File(new File(fatherPath), "2.jpg");
		
		// 绝对路径构造
		File src3=new File("D:/other/3.jpg");
		
		//没有盘符:以user.dir构建
		File src4= new File("4.jpg");
		System.out.println(src4.getName());
		System.out.println(src4.getPath());
		System.out.println(src4.getAbsolutePath());
		
		//.表示当前路径
		File src5= new File(".");
		System.out.println(src5.getName());
		System.out.println(src5.getPath());
		System.out.println(src5.getAbsolutePath());
	}
}

コードの実行結果:

4.JPGの
4.JPGの
D:\ myworkを\ linlinjava \ javalearnig \ JavaStudy \ 4.JPG


D:\ myworkを\ linlinjava \ javalearnig \ JavaStudy。

図2に示すように、情報を取得する方法

ある、getPath():GETファイルパス
のgetName():最後のレイヤ名を取得するには、ドキュメント名が
のgetParent():パスが最後の層を取り除くために、その親ディレクトリのファイル
getParentFileは():新しい親ファイルのパスを取得

		String fatherPath = "D:/other";
		String name = "1.jpg";

		File src1 = new File(fatherPath, name);
		System.out.println("文件路径:" + src1.getPath());
		System.out.println("文件名:" + src1.getName());
		System.out.println("文件上层目录:" + src1.getParent());
		File file1 = src1.getParentFile();// 得到父类路径文件的新对象
		//String[] filesName = file1.list();// 返回路径下文件名的String数组
		//File[] files = file1.listFiles();//获取路径下文件组成的File数组

3、決意方法

isDirectory()は、フォルダかどう
ISFILE()がファイルかどうか
存在する()パスが存在
canWrite()が書き込み可能である
canRead()読み取り可能な
長さ()ファイルの長さは?ファイル、フォルダ、長さかどうかを判断するために使用することができます0

		String fatherPath = "D:/other";
		String name = "1.jpg";
		File src1 = new File(fatherPath);
		File src2 = new File(fatherPath, name);
		System.out.println("是否存在?  "+src1.exists());
		System.out.println("是否是文件夹?  "+src1.isDirectory());
		System.out.println("是否是文件?  "+src2.isFile());
		System.out.println("文件是否可写? " + src2.canWrite());
		System.out.println("文件是否可读? " + src2.canRead());
		System.out.println("是否是绝对路径? " + src1.isAbsolute());
		System.out.println("文件长度?  "+src1.length());// 文件的长度(字节数),文件夹长度为0,只有文件可以读取长度

4、作成/削除ファイル

でCreateNewFileは();ファイルを作成
、削除ファイル、削除に()
のcreateTempFileを();一時ファイルの作成
tempFile.deleteOnExitを();一時ファイルが自動的にプログラム終了後に削除されます

	public static void test3() throws IOException, InterruptedException {
		String path = "D:/other/0.jpg";
		File f1 = new File(path);
		if (!f1.exists()) {
			boolean flag = f1.createNewFile();
			System.out.println(flag ? "创建成功" : "创建失败");
		} else {
			System.out.println("文件已存在!");
		}
		boolean flag = f1.delete();
		System.out.println(flag ? "删除成功" : "删除失败");
		//临时文件.
		File tempFile=File.createTempFile("tes", ".temp", new File("D:/other"));
		Thread.sleep(2000);
		tempFile.deleteOnExit();//程序退出后自动删除
		
	}

5、ディレクトリ操作方法

MKDIR()新しいフォルダを作成し、あなたは一つだけ作成することができ
、新しいフォルダを作成する(mkdirs)を、あなたができる多層
リストは():ファイルまたはフォルダへのパスの配列の名前を返します
)(LISTFILES:次のファイルまたはファイルのパスを返します。ファイルフォルダの配列

/**
*代码功能:输出文件夹下的文件名称。
*输出文件夹下的文件路径。
*输出文件夹中的txt文件路径
**/
public class demo04File {

	public static void main(String[] args) {
		String path = "D:/other/test/mytest";
		File f1 = new File(path);
		f1.mkdir();// 创建目录,必须确保父目录存在,否则失败
		f1.mkdirs();// 创建目录,如果父目录不存在,一同创建
		if (f1.exists()) {
			System.out.println("=====子目录||子文件====");
			String[] names = f1.list();
			for (String temp : names) {
				System.out.println(temp);
			}
			
			System.out.println("=====子目录||子文件的file对象====");
			File[] files=f1.listFiles();
			for(File temp:files){
				System.out.println(temp.getAbsolutePath());
			}
			
			System.out.println("=====子目录||文件txt对象====");
			//命令设计模式
			File[] files1=f1.listFiles(new FilenameFilter() {
				/**
				 * dir代表f1
				 */
				@Override
				public boolean accept(File dir, String name) {
					return new File(dir,name).isFile()&& name.endsWith(".txt");
				}
			});
			for(File temp:files1){
				System.out.println(temp.getAbsolutePath());
			}
		}
	}
}

コードは実行結果です:
=サブファイルのサブディレクトリ||
1.docx
2.mpp
3.xlsx
4.txt
=ファイルオブジェクトのサブディレクトリ||サブファイル
D:\他の\テスト\のMyTest \ 1.docx
D:\他の\テスト\のMyTest \ 2.mpp
D:\他の\テスト\のMyTest \ 3.xlsx
D:\他の\テスト\のMyTest \ 4.txt
=txtファイルは、サブディレクトリオブジェクト||
D:\その他\テスト\のMyTest \ 4.txt

公開された57元の記事 ウォン称賛13 ビュー1132

おすすめ

転載: blog.csdn.net/weixin_42924812/article/details/105053590