字节输入流操作
public class FileInputStreamTest {
public static void main(String[] args) throws Exception{
redTest();
// red2Test();
// red3Test();
// red4Test();
}
public static void redTest() throws IOException{
String path = "E:"+File.separator+"a.sql";
File file = new File(path);
InputStream is = new FileInputStream(file);
// int i;
// while(-1!=(i = is.read())){
// System.out.println(i);
// }
int i;
while((i = is.read())!=-1){
System.out.print((char)i);
}
is.close();
}
public static void red2Test() throws IOException{
//创建文件路径
String path = "E:/a.sql";
//创建文件
File file = new File(path);
//创建一个输入流
InputStream is = new FileInputStream(file);
//创建一个数组,表示每次从文件中读取的字节长度
byte[] b = new byte[1];
//读取文件
int i;
while((i=is.read(b))!=-1){
System.out.println(new String(b));
}
//关闭输入流
is.close();
//System.out.println(new String(b));
//System.out.println(Arrays.toString(b));
}
/**
* 读取文件 去掉多余的空格
* @throws IOException
*/
public static void red3Test() throws IOException{
String path = "e:"+File.separator+"a.sql";
File file = new File(path);
InputStream is = new FileInputStream(file);
byte[] b = new byte[1024];//1024 太大 文件内容很小容易留下空格
int length = is.read(b);//获取文件的长度
is.close();
System.out.println(new String(b,0,length));//只输出文件刚刚好文件的大小
}
/**
* 创建和文件一样大小的数组 用来存放文件
* @throws Exception
*/
public static void red4Test() throws Exception{
String path = "e:"+File.separator+"a.sql";
File file = new File(path);
InputStream is = new FileInputStream(file);
byte[] b = new byte[(int) file.length()];
is.read(b);
is.close();
System.out.println(new String(b));
}
}
字节输出流操作
public class OutputStreamTest {
public static void writerTest() throws IOException{
String path = "E:"+File.separator+"a.sql";
File file = new File(path);
OutputStream os = new FileOutputStream(file,true);//true 代表追加在原来的文件中追加 没有true或者false 代表覆盖
String str = "\r\nhello world";
byte[] b = str.getBytes();
os.write(b);
os.close();
}
public static void copy() throws IOException{
String path1 = "e:"+File.separator+"a.sql";
String path2 = "e:"+File.separator+"b.txt";
File file1 = new File(path1);
File file2 = new File(path2);
InputStream is = new FileInputStream(file1);
OutputStream os = new FileOutputStream(file2);
if(!file1.exists()){
System.out.println("被复制的文件不存在");
System.exit(0);
}
if(is!=null && os!=null){
int temp;
while((temp=is.read())!=-1){
os.write(temp);
}
}
is.close();
os.close();
}
/**
* 压缩文件
* @throws IOException
*/
public static void zip() throws IOException{
String path1 = "e:"+File.separator+"a.sql";
File file = new File(path1);
File file1 = new File("e:"+File.separator+"a.zip");
InputStream is = new FileInputStream(file);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1) );
zos.putNextEntry(new ZipEntry(file.getName()));
zos.setComment("hello");
int temp=0;
while((temp=is.read())!=-1){
zos.write(temp);
}
is.close();
zos.close();
}
/**
* 同时压缩多个文件
* @throws IOException
*/
public static void zipMany() throws IOException{
String path = "E:/API";
File file = new File(path);
File zipFile = new File(path+File.separator+"API.ZIP");//创建一个压缩文件
File[] file1 = file.listFiles();//获取路径下所有的文件
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));//创建一个压缩流
InputStream is;
byte[] b = new byte[1024];//每次读取1M
int cout = 0;
for(int i=0;i<file1.length;i++){
is=new FileInputStream(file1[i]);
System.out.println(file.getName());
System.out.println(file1[i].getName());
//new ZipEntry(file1[i].getName()) 创建压缩包中的文件的名称
cout++;
System.out.println(cout);
zos.putNextEntry(new ZipEntry(String.valueOf(cout)));//将文件放入压缩包
int temp=0;
while((temp=is.read(b))!=-1){
zos.write(temp);
}
is.close();
}
zos.close();
}
/**
* 获取压缩文件名称
* @throws IOException
* @throws IOException
*/
public static void showZip() throws IOException, IOException{
File file = new File("E:/API/API.ZIP");
ZipFile zf = new ZipFile(file);
System.out.println(zf.getName());
}
/**
* 列出指定目录的全部文件(包括隐藏文件)
*/
public static void showAllFile(){
String path = "e:"+File.separator;
File file = new File(path);
File[] file1 = file.listFiles();
for (File file2 : file1) {
System.out.println(file2);
}
}
/**
* 创建一个文件夹
*/
public static void CreatFloder(){
String path = "D:"+File.separator+"hello";
File file = new File(path);
file.mkdir();
}
/**
* 判断一个路径是否为目录
*/
public static void isDirectory(){
String path = "D:"+File.separator+"hello1";
File file = new File(path);
if(file.isDirectory()){
System.out.println(true);
}else{
System.out.println(false);
}
}
/**
* 删除文件 文件夹
*/
public static void delete(){
String path = "D:"+File.separator+"hello";
File file = new File(path);
if(file.exists()){
file.delete();
}
}
/**
* 搜索指定目录的全部内容
* @param file
*/
public static void getAllFiles(File file){
// String path = "e:"+File.separator+"apache-tomcat-7.0.67";
// File file = new File(path);
if(file!=null ){
if(file.isDirectory()){
File[] fileArray = file.listFiles();
for(int i=0;i<fileArray.length;i++){
getAllFiles(fileArray[i]);//递归调用
}
}else{
System.out.println(file);
}
}
}
public static void main(String[] args) throws IOException {
//writerTest();
//zip();
//zipMany();
// showZip() ;
// showAllFile();
// CreatFloder();
//isDirectory();
// System.out.println(File.separator);
// System.out.println(File.separatorChar);
// System.out.println(File.pathSeparator);
// System.out.println(File.separatorChar);
// delete();
String path = "e:"+File.separator+"apache-tomcat-7.0.67";
File file = new File(path);
getAllFiles(file);
}
}
字符流操作
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* 字符流操作
* @author John
*
*/
public class ReadAndWriter {
/**
* 字符流读取文件
* @throws IOException
*/
public static void readTest1() throws IOException{
String path="e:"+File.separator+"a.txt";
File file = new File(path);
Reader reader = new FileReader(file);
char[] ch = new char[1024];
reader.read(ch);
reader.close();
System.out.println(new String(ch));
}
/**
* 字符流读取文件
* @throws Exception
*/
public static void readTest2() throws Exception{
String path ="e:"+File.separator+"a.txt";
File file = new File(path);
char[] ch = new char[(int) file.length()];//创建和文件大小的相等的字符数组
Reader reader = new FileReader(file);
reader.read(ch);
reader.close();
System.out.println(new String(ch));
}
/**
* 字符流读取文件
* @throws IOException
*/
public static void readTest3() throws IOException{
String path ="e:"+File.separator+"a.txt";
File file =new File(path);
char[] ch = new char[1024];
int lem = (int) file.length();//获取文件的长度
Reader reader = new FileReader(file);
reader.read(ch);
reader.close();
System.out.println(new String(ch,0,lem));
}
/**
* 字符流读取文件
* @throws IOException
*/
public static void readTest4() throws IOException{
String path = "e:"+File.separator+"a.txt";
File file = new File(path);
Reader reader = new FileReader(file);
char[] ch = new char[1];
int temp;
while((temp=reader.read(ch))!=-1){//读到文件末尾自动停止
System.out.print(new String(ch));
}
}
public static void main(String[] args) throws Exception {
readTest4();
}
}
五种方法实现字符流复制文件
/**
* 五种方法实现字符流复制文件
* @author John
*
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
method5();
}
//用字符缓存流的特殊方法
//readLine()一次读取一行
//newLine(),换行
private static void method5() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("陈二狗的妖孽人生-精校版.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("陈二狗的妖孽人生-精校版copy.txt"));
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
//用字符缓冲流 一个数组复制文件
private static void method4() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("陈二狗的妖孽人生-精校版.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("陈二狗的妖孽人生-精校版copy.txt"));
char[] c = new char[1024];
int len=0;
while((len=br.read(c))!=-1){
bw.write(c,0,len);
bw.flush();
}
bw.close();
br.close();
}
//用字符缓冲流 一个字符一个字符复制文件
private static void method3() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("陈二狗的妖孽人生-精校版.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("陈二狗的妖孽人生-精校版copy.txt"));
int len=0;
while((len=br.read())!=-1){
bw.write(len);
}
bw.close();
br.close();
}
//使用FileReader一个数组复制文件
private static void method2() throws IOException {
FileReader fr = new FileReader("陈二狗的妖孽人生-精校版.txt");
FileWriter fw = new FileWriter("陈二狗的妖孽人生-精校版copy.txt");
char[] c = new char[1024];
int len=0;
while((len=fr.read(c))!=-1){
fw.write(c,0,len);
}
fw.close();
fr.close();
}
//使用FileReader一个字符一个字符复制文件
private static void method1() throws IOException {
FileReader fr = new FileReader("陈二狗的妖孽人生-精校版.txt");
FileWriter fw = new FileWriter("陈二狗的妖孽人生-精校版copy.txt");
int len=0;
while((len=fr.read())!=-1){
fw.write(len);
}
fw.close();
fr.close();
}
}
复制单级文件夹:即复制文件夹中所有的文件
/**
* 复制单级文件夹:即复制文件夹中所有的文件
* @author John
*
*/
public class CopyFiles {
public static void main(String[] args) throws IOException{
//创建要复制的文件夹
File srcFolder = new File("F:/BaiduYunDownload/demo");
//创建要复制的目的地
File destFolder= new File("demo");
//判断目的地是否存在,不存在则创建
if(!destFolder.exists()){
destFolder.mkdir();
}
//获取所要复制的文件夹中所有的文件
File[] file = srcFolder.listFiles();
//遍历文件
for (File file2 : file) {
//将文件封装成一个新的文件
File file3 = new File(destFolder,file2.getName());
//复制文件
copy(file2,file3);
}
}
private static void copy(File file2, File file3) throws IOException{
//因为文件夹中有多种格式的文件,所以采用字节流
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file2));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file3));
int len;
byte[] b =new byte[1024];
while((len=bis.read(b ))!=-1){
bos.write(b,0,len);
}
bos.close();
bis.close();
}
}
复制文件夹下所有后缀为.java的文件,并重命名
复制文件夹下所有后缀为.java的文件,并重命名
/**
* 复制文件夹下所有后缀为.java的文件,并重命名
* @author John
*
*/
public class CopyFilesAndRename {
public static void main(String[] args) throws IOException{
//创建源文件夹
File srcFolder = new File("F:/BaiduYunDownload/java");
//创建目标文件夹
File destFolder = new File("java");
//判断目标文件夹是否存在 不存在则创建
if(!destFolder.exists()){
destFolder.mkdir();
}
//过滤源文件夹中以.java结尾的文件
File[] files = srcFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir,name).isFile() && name.endsWith(".java");
}
});
//遍历文件
for (File file : files) {
File file2 = new File(destFolder,file.getName());
//复制文件
copy(file,file2);
//获取复制后的文件名,将java替换成jad
String name = file2.getName().replace(".java", ".jad");
File file3 = new File(destFolder,name);
//重命名
file2.renameTo(file3);
}
}
private static void copy(File file, File file2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
int len;
byte[] b = new byte[1024];
while((len=bis.read(b))!=-1){
bos.write(b,0,len);
}
bos.close();
bis.close();
}
}
复制整个文件夹 里面包含文件夹和文件
/** * 复制整个文件夹 里面包含文件夹和文件 * @author John * */ public class CopyFlodersAndFiles { public static void main(String[] args) throws IOException { //要复制的文件 File srcFile = new File("D:\\Program Files\\WPS Office"); //要复制到哪里去 File destFile= new File("Java"); copyFile(srcFile,destFile); } //复制 private static void copyFile(File srcFile, File destFile) throws IOException { //判断是否存在destFile这个文件夹 if(!destFile.exists()){ //不存在则创建 destFile.mkdir(); } //判断是否存在srcFile if(srcFile!=null){ //如果是文件夹 if(srcFile.isDirectory()){ //在destFile里面 创建相同名称的文件夹 File srcFile1 = new File(destFile,srcFile.getName()); //判断是否存在这样的文件夹 if(!srcFile1.exists()){ //不存在则创建 srcFile.mkdir(); } //遍历要复制的文件夹 File[] filelist = srcFile.listFiles(); for (File file : filelist) { //递归,继续判断是否是文件还是文件夹 copyFile(file,srcFile1); } }else{ //如果是文件,直接复制 File newFile = new File(destFile,srcFile.getName()); copy(srcFile,newFile); } } } /** * 复制文件 * @param srcFile * @param newFile * @throws IOException */ private static void copy(File srcFile, File newFile) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(newFile)); byte[] b = new byte[1024]; int len; while((len=bis.read(b))!=-1){ bos.write(b, 0, len); } bos.close(); bis.close(); } }