File
1.FIle对象表示路径,可以指向文件/文件夹;可以是存在的,也可以是不存在的;
2.三种构造方式
3.相对路径:默认当前项目下不带盘符的路径;
绝对路径:带盘符的路径。
相关方法及练习代码
关于在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 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;
}