目录
文件路径:从文件系统这个树上,找到唯一确定得位置,这个节点可以不存在。
绝对路径:从文件树得根节点出发,找到某一个文件得节点位置,就为绝对路径
相对路径:从当前文件节点出发,另一个节点相对于当前节点得位置
硬件体系:处理器CPU,存储器(内存),输入设备,输出设备
硬盘——存储:
各个硬件读写速度的比较
文件路径:从文件系统这个树上,找到唯一确定得位置,这个节点可以不存在。
绝对路径:从文件树得根节点出发,找到某一个文件得节点位置,就为绝对路径
相对路径:从当前文件节点出发,另一个节点相对于当前节点得位置
. . :表示回到上一级目录
. :表示当前目录
当前目录:进程得启动目录,
路径得描述:
绝对路径:\ : 路径分隔符
window:D:\ files \ a.txt 根盘符 : \ 文件名 \ 文件.后缀名
linux:/ mac: D: / 文件夹 / 文件名
相对路径:进程启动时的目录
public class 相对路径 {
public static void main(String[] args) throws IOException {
File file = new File("./a.txt");
System.out.println(file.getCanonicalPath());
}
}
当前在file文件夹 : 描述a.txt 就为 : file\a.txt
代码中得路径: windows中 \ 表示转义 ,所以 windows中得代码: D:\\file\\a.txt
java是一个跨平台得语言,所以代码中可以不区分正反斜杠
File类:
构造方法
普通方法:
删除文件delete()
只能删除叶子节点或者空得文件夹;
deleteOnExit() : 在程序退出时删除文件;
删除非空得文件夹,只能先将文件夹删除为空,才能删除该文件夹
文件得创建与判断
windows上删除文件相当于剪切复制,将文件从当前节点复制回收站中,
delete()方法是将文件系统中真正的删除掉,不进入回收站
package com.yyk.io.IO操作文件;
import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Date;
import java.util.Deque;
public class FileMetohd {
public static void main(String[] args) throws IOException {
File file = new File("D:\\IO\\.\\..\\..\\IO\\hello.txt");
System.out.println("文件存在吗:"+file.exists());
System.out.println("是文件夹吗:"+file.isDirectory());
System.out.println("是文件吗" + file.isFile());
System.out.println("***********在该路径下创建hello.txt*******");
file.createNewFile();
System.out.println("文件存在吗:"+file.exists());
System.out.println("是文件夹吗:"+file.isDirectory());
System.out.println("是文件吗" + file.isFile());
System.out.println();
System.out.println("********获取文件得路径**********");
System.out.println("文件得绝对路径:" +file.getAbsoluteFile());//返回得是文件
System.out.println("文件得绝对路径:" +file.getAbsolutePath());//返回得是字符串
System.out.println("文件得权威绝对路径:" +file.getCanonicalFile());//返回得是文件,将中间的没有意义的点去掉
System.out.println("文件得权威绝对路径:" +file.getCanonicalPath());//返回得是字符串,去掉中间的文件..和.
System.out.println("文件名:" + file.getName());
System.out.println("文件的父路径:" + file.getParent());
System.out.println("文件的父路径的权威路径:" + file.getParentFile().getCanonicalPath());
System.out.println("文件的大小:" + file.length());
System.out.println("用绝对路径创建的文件吗:" +file.isAbsolute());
System.out.println("文件被隐藏吗:" +file.isHidden());
System.out.println("文件最后一次被修改的时间:" + new Date(file.lastModified()));
System.out.println("该文件的父目录下的文件个数:" + Arrays.toString(file.getParentFile().listFiles()));
System.out.println();
System.out.println("********文件的深度优先遍历***********");
DFSFilePath(file.getParentFile());
System.out.println();
System.out.println("********文件的广度优先遍历***********");
NFSFilePath(file.getParentFile());
System.out.println("文件系统的根目录" + file.listRoots());
System.out.println();
System.out.println("*****mkdir****创建文件夹*******mkdirs****");
File file1 = new File("D:\\IO\\A\\BB\\CCC");
file1.mkdirs();
System.out.println("mkdirs创建不存在路径下的文件夹" + file1.getCanonicalPath());
File file2 = new File("D:\\IO\\D");
file2.mkdir();
System.out.println("mkdir在已经存在的目录下创建文件夹:" + file2.getCanonicalPath());
System.out.println("重命名文件:" );
File dest = new File("D:\\IO\\你好.txt");
System.out.print(file.getName()+" 被重命名为 ");
file.renameTo(dest);
dest.delete();//删除文件夹
System.out.println(dest.getName());
System.out.println("文件的重命名相当于文件的剪切复制");
}
/**
* 文件的深度优先遍历
* @param dir
*/
public static void DFSFilePath(File dir) throws IOException {
File[] files = dir.listFiles();
if (dir == null){
return;
}
for (File file: files) {
if (file.isDirectory()){
System.out.println(dir.getCanonicalPath()+"目录下的"+file.getName()+"文件夹");
DFSFilePath(file);
}else {
System.out.println(dir.getCanonicalPath()+"目录下的"+file.getName()+"文件");
}
}
}
/**
* 文件广度优先遍历
* @param dir
* @throws IOException
*/
public static void NFSFilePath(File dir) throws IOException {
Deque<File> fileDeque = new ArrayDeque<>();
fileDeque.offer(dir);
while (!fileDeque.isEmpty()){
File file = fileDeque.poll();
if (file.isDirectory()){
File[] files = file.listFiles();
for (File f:files) {
fileDeque.offer(f);
}
System.out.println("文件夹:" + file.getCanonicalPath());
}else {
System.out.println("文件:" + file.getCanonicalPath());
}
}
}
}
文件的读写
文件的读数据:FileInputStream
1,以字节流形式进行文件的读操作
概念模型:
2,以字符流方式进行文件的读操作(文本方式的文件)
无论是字节流还是字符流,都是用Scanner类来读取数据
public class 读数据都用Scanner {
public static void main(String[] args) throws Exception{
try(InputStream is = new FileInputStream("./hello.txt")){
try(Scanner scanner = new Scanner(is,"UTF-8")){
while (scanner.hasNextLine()){
String next = scanner.nextLine();
System.out.println(next);
}
}
}
}
}
写OutputStream:
写数据在关闭资源的时候,需要对缓冲区进行刷盘,避免缓冲区中还未写入的数据。
文件的复制
public class 文件复制 {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("D:\\IO\\12.jpg");
byte[] buff = new byte[10240];//缓冲区,用来匹配内存和硬盘之间读写速度的差距
OutputStream os = new FileOutputStream("D:\\IO\\34.jpg");
while (true){
int read = is.read(buff);
if (read == -1){//如果是EOS,便是没有数据了,直接退出
break;
}
os.write(buff);//将读到缓冲区的字节全部写入文件中
os.flush();//最后冲刷缓冲区
}
}
}
文件夹的复制
package com.yyk.io.IO操作文件;
import java.io.*;
public class 文件夹复制 {
static File srcFile = new File("D:\\IO\\A");//源文件目录
static File tarFile = new File("D:\\IO\\D"); //目标文件目录
public static void main(String[] args) throws Exception{
copy(srcFile);
}
private static void copy(File dirFile) throws Exception {
File[] files = dirFile.listFiles();
if (files == null){
return;
}
for (File file:files) {
String filePath = file.getCanonicalPath();//当前文件的目录
String tarFilePath = tarFile.getCanonicalPath();//需要复制的文件的根目录
String aimPath= tarFilePath + filePath.substring(srcFile.getCanonicalPath().length());//需要复制的目录
File aimFile = new File(aimPath);
if (file.isDirectory()) {//如果是文件夹,创建文件夹
aimFile.mkdir();
//就继续遍历
copy(file);
}else if (file.isFile()){
copyFile(file,aimFile);
}
}
}
public static void copyFile(File file,File aimFile) throws Exception {
InputStream is = new FileInputStream(file);
byte[] buff = new byte[10240];//缓冲区,用来匹配内存和硬盘之间读写速度的差距
OutputStream os = new FileOutputStream(aimFile);
while (true){
int read = is.read(buff);
// System.out.println("读到了"+read+"个字节");
if (read == -1){//如果是EOS,便是没有数据了,直接退出
break;
}
os.write(buff,0,read);//将读到缓冲区的字节全部写入文件中
os.flush();//最后冲刷缓冲区
}
}
}
模拟OJ测试文档的编写
package com.yyk.io.IO操作文件.oj;
public class Node {
int val ;
Node next;
public Node(int val){
this.val = val;
}
}
package com.yyk.io.IO操作文件.oj;
public class Solution {
public Node reverse(Node head){
Node prev = null;
Node cur = head;
while (cur != null){
Node next = cur.next;
cur.next = prev;
prev = cur;
cur =next;
}
return prev;
}
}
package com.yyk.io.IO操作文件.oj;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
public class 测试用例的检验 {
static Solution solution = new Solution();
static File inputFile = new File("D:\\IO\\C\\input.txt");
static File resFile = new File("D:\\IO\\C\\output.txt");
public static void main(String[] args){
try{
//按行读输入输出文件
InputStream is = new FileInputStream(inputFile);//输入
InputStream res = new FileInputStream(resFile);//结果
Scanner isSc = new Scanner(is,"UTF-8");
Scanner resSc = new Scanner(res,"UTF-8");
while (isSc.hasNextLine()){
String inStr = isSc.nextLine();
String resStr = resSc.nextLine();
test(inStr,resStr);
}
System.out.println("通过所有测试用例");
}catch (Exception e){
}
}
private static void test(String inStr,String resStr){
StringBuilder sb = new StringBuilder();
try {
//将instr按找空格隔开,转为int,创建链表然后反转
String[] strs = inStr.split(" ");
Node head = new Node(-1);
Node tem = head;
for (String s:strs) {
Node node = new Node(Integer.parseInt(s));
tem.next = node;
tem = node;
}
Node newNode = solution.reverse(head.next);//反转后的新链表
//遍历链表转为字符串
while (newNode != null){
sb.append(newNode.val + " ");
newNode = newNode.next;
}
//最后去掉最后一个空格
sb.delete(sb.length() - 1,sb.length());
if (!resStr.equals(sb.toString())){
System.out.println("输入用例:" + inStr);
System.out.println("期望输出:" + resStr);
System.out.println("实际输出:" + sb.toString());
System.exit(1);
}
}catch (Exception e){
System.out.println("输入用例:" + inStr);
System.out.println("期望输出:" + resStr);
System.out.println("实际输出:" + sb.toString());
e.printStackTrace();
}
}
}