查找某目录下以avi结尾的大于100M的文件
public class Demo {
public static void main(String[] args) {
File e = new File("e://");
File[] files = e.listFiles();
listFile(files);
}
public static void listFile(File[] files){
if(files!=null&&files.length>0){
for (File file:files) {
if(file.isFile()){
if(file.getName().endsWith(".avi")){
if(file.length()>100*1024*1024)
System.out.println("找到了一个avi文件"+file.getAbsolutePath());
}
}else {
File[] files2 = file.listFiles();
listFile(files2);
}
}
}
}
}
字节流输出
public class Demo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("c://a.txt");
byte[] bytes2 = "ABCDEF".getBytes();
fos.write(bytes2,2,2);
fos.close();
System.out.println("已经写出");
}
}
字节流读入
public class Demo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("c://a.txt");
byte[] bytes = new byte[10];
int len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
fis.close();
}
}
给文件做异或加密
public class Demo {
public static void main(String[] args) throws IOException {
System.out.println("请输入文件的全路径");
Scanner scanner = new Scanner(System.in);
String filename = scanner.nextLine();
File oldFile = new File(filename);
File newFile = new File(oldFile.getParent(),"mi-"+oldFile.getName());
FileInputStream fis = new FileInputStream(oldFile);
FileOutputStream fos = new FileOutputStream(newFile);
while (true){
int b = fis.read();
if(b==-1){
break;
}
fos.write(b^10);
}
System.out.println("加密或解密完成");
}
}
字符流输出
public class Demo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("c://b.txt",true);
fw.append("锄禾日当午").append(",").append("汗滴禾下土");
fw.write("锄禾日当午");
fw.flush();
fw.close();
}
}
字符流读取
public class Demo10 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("b.txt");
while (true){
int c = fr.read();
if(c==-1){
break;
}
System.out.println((char)c);
}
char[] chars = new char[100];
System.out.println(chars[0]);
fr.close();
}
}
字节流转字符流
public class Demo11 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("c://a.txt");
InputStreamReader isr = new InputStreamReader(fis,"gbk");
while (true){
int c = isr.read();
if(c==-1){
break;
}
System.out.println((char) c);
}
}
}
收集异常信息
public class Demo12 {
public static void main(String[] args) throws FileNotFoundException {
try {
String s = null;
s.toString();
}catch (Exception e){
PrintWriter pw = new PrintWriter("c://bug.txt");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
pw.print(format.format(new Date()));
e.printStackTrace(pw);
pw.close();
}
}
}
properties文件读写
public class Demo13 {
public static void main(String[] args) throws IOException {
Properties pt = new Properties();
Reader fw = new FileReader("c://book.properties");
pt.load(fw);
System.out.println("name");
System.out.println("info");
}
}
序列化与反序列化
public class Demo14 {
public static void main(String[] args) throws IOException {
Book b = new Book("金苹果","讲述了种植过程");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c://book.hahas"));
oos.writeObject(b);
oos.close();
}
static class Book implements Serializable {
private String name;
private String info;
public Book(String name, String info) {
this.name = name;
this.info = info;
}
public Book() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", info='" + info + '\'' +
'}';
}
}
}
JDK9特性
public class Demo15 {
public static void main(String[] args) throws FileNotFoundException {
FileReader fr = new FileReader("c://book.txt");
PrintWriter pw = new PrintWriter("c://book.txt");
try(fr;pw){
int c = fr.read();
System.out.println((char)c);
}catch (IOException e) {
e.printStackTrace();
}
}
}
序列化反序列化工具类实现
public class MySerializableUtils {
public static void mySerializable(Object obj) throws IOException {
OutputStream outputStream = new FileOutputStream("expressStore.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(obj);
objectOutputStream.close();
}
public static ExpressHouse myDeserializable() throws IOException, ClassNotFoundException {
File file = new File("expressStore.txt");
if(!file.exists()) {
file.createNewFile();
}
if(file == null || file.length()==0){
ExpressHouse expressHouse = new ExpressHouse();
expressHouse.setLocationMap(new LinkedHashMap<Integer, Express>());
return expressHouse;
}
InputStream inputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
Object obj = objectInputStream.readObject();
if(obj instanceof ExpressHouse) {
return (ExpressHouse)obj;
}
return null;
}
}
Print与BufferedReader
FileReader fr = new FileReader("d:\\d.txt");
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
System.out.println(str);