File
1.FIle对象表示路径,可以指向文件/文件夹;可以是存在的,也可以是不存在的;
2.三种构造方式
public File(String pathname) 把字符串表示的路径变成File对象
public FIle(String parent, String child) 把父级路径和子级路径进行拼接
public File(File parent, String child) 把父级路径和子级路径进行拼接
3.相对路径:默认当前项目下不带盘符的路径;
绝对路径:带盘符的路径。
相关方法及练习代码
import java.io.File; import java.io.IOException; public class demo01 { public static void main(String[] args) throws IOException { File f1 = new File("C:\\Users\\nine position\\Desktop\\ai tag.txt"); long len1 = f1.length(); // 返回文件大小(单位为字节) ps:无法获取文件夹大小 System.out.println(len1); File f2 = new File("C:\\Users\\nine position\\Desktop\\ai tag.txt"); String path1 = f2.getAbsolutePath(); // 获取绝对路径 System.out.println(path1); File f3 = new File("C:\\Users\\nine position\\Desktop\\ai tag.txt"); String name1 = f3.getName(); // 获取文件(夹)名字 System.out.println(name1); File f4 = new File("F:\\1刷题相关"); // 获取当前路径下的所有内容 ps:路径不存在/路径为文件 返回NULL; 路径下为空,返回一个长度为0的数组 File[] files = f4.listFiles(); for(File file : files) { System.out.println(file); } File f5 = new File("File\\aaa"); f5.mkdirs(); //拼接父级路径和子级路径 File f6 = new File(f5, "a.txt"); boolean b = f6.createNewFile(); if(b) System.out.println("YES"); else System.out.println("NO"); } }
关于在JAVA中使用File查找某个路径下的同类型/带某个关键字的文件
思路:
递归;从而得到某个路径下的所有文件,再查找文件名;
import java.io.File;
public class Test03 {
public static void main(String[] args) {
find();
}
public static void find()
{
File[] arr = File.listRoots(); // 获取所有根目录的文件的对象数组
for(File f : arr){
findMP4(f);
}
}
public static void findMP4(File src) {
//1.进入文件夹
File[] files = src.listFiles();
//2.遍历,得到所有文件夹或文件
if(files != null)
{
for(File f : files)
{
if(f.isFile()){
String name = f.getAbsolutePath();
if(name.endsWith(".mp4"))System.out.println(name);
}else{
findMP4(f); //递归调用
}
}
}
}
}
IO流
1. 基本概念:存取和读取数据的解决方案
2.分类
- 按流向分类:输入流,输出流
- 按操作文件类型分类:字节流(可以操作所有文件,单位为字节),
字符流(仅能操作纯文本文件);
3.字节流基本步骤:创建字节流对象->写数据->释放资源;
相关方法及练习代码
--------以FileIO为例
文件连续写入内容
package bytestream01; import java.io.FileOutputStream; import java.io.IOException; public class ByteStreamDemo03 { public static void main(String[] args) throws IOException { //续写:new fos时的第二个参数,默认为false,可手动赋值为true FileOutputStream fos =newFileOutputStream( "F:\\IdeaProjects\\myio\\a.txt", true); //创建基本的文件输出流,参数为输出流路径 String str = "sjfoahi"; //方法获取byte数组 byte[] bytes = str.getBytes(); fos.write(bytes); //基本的写入操作 //换行写:再写一个换行符 //Windows: \r\n //Linux: \n //Mac: \r String wrap = "\r\n"; byte[] bytes1 = wrap.getBytes(); fos.write(bytes1); String str2 = "666"; byte[] bytes2 = str2.getBytes(); fos.write(bytes2); fos.close(); //关流(释放资源) } }
文件拷贝
单字节版本
package bytestream02; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ByteStreamDemo03 { public static void main(String[] args) throws IOException { FileInputStream fis = newFileInputStream( "F:\\IdeaProjects\\myio\\a.txt"); FileOutputStream fos = new FileOutputStream( "F:\\IdeaProjects\\myio\\copy.txt"); long l1 = System.currentTimeMillis(); //关键:边读边写 int b; //中间变量 while((b = fis.read()) != -1){ //输入流读取操作,每次读取后,此对象的read指针后移一位 //读取失败,返回值为1; fos.write(b); //输出流写入 } long l2 = System.currentTimeMillis(); System.out.println(l2 - l1 + "ms"); //先开的流后关 fos.close(); fis.close(); } }
优化版本(多字节读写)
package bytestream02;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//通过字节数组读入即可
public class ByteStreamDemo04 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("F:\\IdeaProjects\\myio\\a.txt");
FileOutputStream fos = new FileOutputStream("F:\\IdeaProjects\\myio\\copy.txt");
long l1 = System.currentTimeMillis();
byte[] bytes = new byte[1024 * 1024 * 5];
int len1;
while((len1 = fis.read(bytes)) != -1) {
System.out.println(len1);
fos.write(bytes, 0, len1);
}
long l2 = System.currentTimeMillis();
System.out.println(l2 - l1 + "ms");
}
}
Problem - C - Codeforceshttps://codeforces.com/contest/1933/problem/C题目分析:
暴力+Map(数组也能过,会慢很多);以a,b为基础遍历所有l下的可能构成的数,再计算合法的k值,然后将出现的k用map记录,计数器++;最后输出计数器即可;
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 3e6 + 9;
bool vis[N];
int qmi(int a, int b)
{
int res = 1;
while(b)
{
if(b & 1) res = res * a;
a *= a; b >>= 1;
}
return res;
}
void solve()
{
memset(vis, 0, sizeof(vis));
int l, a, b; cin >> a >> b >> l;
int cnt = 0;
for(int i = 0; l >= qmi(a, i); i++)
{
for(int j = 0; l >= qmi(b, j); j++)
{
ll temp = qmi(a, i) * qmi(b, j);
if(temp > l) break;
if(l % temp == 0) {
if(!vis[temp]) {
cnt++;
vis[temp] = 1;
}
}
}
}
cout << cnt << '\n';
return ;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0),cout.tie(0);
int _; cin >> _;
while(_--) solve();
return 0;
}