file对象名.mkdir(); 只能创建一级文件夹 ; boolean
file对象名.mkdirs(); 可以创建多级目录;
boolean
file对象名.createnEewFile(); 创建文件;
boolean
file对象名.delete(); 删除文件;
boolean
file对象名.renmeTo(new File(""))
字节流可以读取任意类型数据
抽象基类:InputStream OutStream
字符流只可以读取文本数据
抽象类型:Reader、Writer
写入硬盘
package FIO; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; //文件输出流 public class Demo03_FileOutputStream { public void testFileOutputStream(){ FileOutputStream output=null; try { output=new FileOutputStream("D:/input.txt",true);//true原有的文本不会被覆盖,可以追加 // output.write('w'); //第一种写入 // output.write('z'); // output.write('x'); String str ="学习使我快乐"; // output.write(str.getBytes());//第二种写入 output.write(str.getBytes(),1,2);//第三种写入 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { if(output!=null) output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Demo03_FileOutputStream aa1=new Demo03_FileOutputStream(); aa1.testFileOutputStream(); } }
red 方法可以多次调用,调用后字符是一个一个输出的。
输入输出流
最后要
package com.studyjava.chapter5;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Practice6 {
public static void main(String[] args) {
charCounter("D:\\dev\\test\\bw\\copy.txt");
}
public static void charCounter(String filepath) {
FileReader fr = null;
try {
fr = new FileReader(filepath);
int c = -1;
int up = 0;
int low = 0;
while ( (c=fr.read()) != -1) {
if(c==(int)'A')
up++;
if(c==(int)'a')
low++;
}
System.out.println("'A' shows "+up+" times, 'a' shows "+low+" times.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(fr!=null)
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5,复制一个文件夹里面的所有内容到另外一个文件夹,包括里面的子文件夹
package com.studyjava.chapter5;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Practice5 {
public static void main(String[] args) {
copyFilesWithsubFloder("D:\\dev\\test\\bw\\2\\", "D:\\dev\\test\\bw\\3\\");
}
public static void copyFilesWithsubFloder(String source, String target) {
File src = new File(source);
File dest = new File(target);
if (src.isDirectory()) {
for(File f: src.listFiles()) {
if(f.isFile())
copyFile(f, new File(target, f.getName()));//通过目标路径和文件名构造目标File文件对象,然后copy
else {
File newFloder = new File(target, f.getName());
if(!newFloder.exists())
newFloder.mkdir();//根据目标文件夹路径和源文件夹名称在目标文件夹路径建立文件夹
copyFilesWithsubFloder(f.getAbsolutePath(),newFloder.getAbsolutePath());//复制动作
}
}
}
else {
System.out.println("the source should be a floder.");
}
}
public static void copyFile(File src, File dest) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(src));
bos = new BufferedOutputStream(new FileOutputStream(dest));
byte[] bys = new byte[1024];
int length = -1;
while ((length = bis.read(bys))!=-1) {
bos.write(bys, 0, length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(bis!=null)
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(bos!=null)
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4,根据两个文件夹名称,将A文件夹内容全部拷贝到B文件夹(不包含A文件夹里面的子文件夹)的另一种方法。
package com.studyjava.chapter5;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Practice4 {
public static void main(String[] args) {
copyFilesToFloder("D:\\dev\\test\\bw\\1\\", "D:\\dev\\test\\bw\\2\\");
}
public static void copyFilesToFloder(String source, String target) {
File src = new File(source);
File dest = new File(target);
if (src.isDirectory()) {
File[] files = src.listFiles();
for(File f: files) {
if(f.isFile())
copyFile(f, new File(target, f.getName()));//通过目标路径和文件名构造目标File文件对象
}
}
else {
System.out.println("the source should be a floder.");
}
}
public static void copyFile(File src, File dest) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(src));
bos = new BufferedOutputStream(new FileOutputStream(dest));
byte[] bys = new byte[1024];
int length = -1;
while ((length = bis.read(bys))!=-1) {
bos.write(bys, 0, length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(bis!=null)
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(bos!=null)
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
table
shift+table
老师讲的真的很细哦,学到了!
HashSet:无序的,自动去重
toArray()可以将ArrayList集合对象变成数组
import java.util.
使用泛型编程,在Java里要使用 引用类型
如ArrayList<Integer> al=new ArrayList<Integer>();
for(Object o: al){}//它会取的垢面数组或集合中的每一个数据,然后可以把取得的每一个数据赋值给前面
test1=new OutputStreamWriter((new FileOutputStream("F:\\test11\\Dome9.txt")),"GBK");//
用BufferedInoutStrean第一次用read()读取时只跟硬盘交互一次,读取一个字节,读取很多字节到缓冲区