流与文件
package StreamAndfile;
import java.io.File;
import java.util.ArrayList;
public class Test1 {
static long size=0;
private static ArrayList<String> filelist=new ArrayList<String>();
public static void main(String[] args) {
String filePath = "D://Test1";
Test1.getFiles(filePath);
System.out.println("文件夹的总容量为"+size);
}
static void getFiles(String filePath) {
int i = 0;
File f=new File(filePath);
File[] files=f.listFiles();
for(File file:files) {
if(file.isDirectory()) {
getFiles(file.getAbsolutePath());
filelist.add(file.getAbsolutePath());
} else {
System.out.println(files[i].getAbsolutePath());
long sum = file.length();
size += file.length();
System.out.println("本文件的大小为"+sum);
i++;
}
}
}
}
import java.util.ArrayList;
public class Test1 {
static long size=0;
private static ArrayList<String> filelist=new ArrayList<String>();
public static void main(String[] args) {
String filePath = "D://Test1";
Test1.getFiles(filePath);
System.out.println("文件夹的总容量为"+size);
}
static void getFiles(String filePath) {
int i = 0;
File f=new File(filePath);
File[] files=f.listFiles();
for(File file:files) {
if(file.isDirectory()) {
getFiles(file.getAbsolutePath());
filelist.add(file.getAbsolutePath());
} else {
System.out.println(files[i].getAbsolutePath());
long sum = file.length();
size += file.length();
System.out.println("本文件的大小为"+sum);
i++;
}
}
}
}
![](https://img2018.cnblogs.com/blog/1279396/201811/1279396-20181119130912208-1537314514.png)
package 文件操作流;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileAddMIma {
private static final int numOfEncAndDec=0x99;//加密解密密钥
private static int dataOfFile=0;//文件字节内容
private static final int numOfEncAndDec=0x99;//加密解密密钥
private static int dataOfFile=0;//文件字节内容
public static void main(String[] args) {
File srcFile=new File("D:\\JAVAfiles\\X1.txt");//初始化文件
File encFile=new File("D:\\JAVAfiles\\X2.txt"); //加密文件
File decFile=new File("D:\\JAVAfiles\\X3.txt"); //解密文件
try {
EncFile(srcFile,encFile); //加密操作
DecFile(encFile,decFile);
}catch(Exception e) {
e.printStackTrace();
}
}
private static void EncFile(File srcFile,File encFile)throws Exception{
if(!srcFile.exists()) {
System.out.println("source file not exixt");
}
if(!encFile.exists()) {
System.out.println("encrypt file created");
encFile.createNewFile();//若无加密文件,新建一个加密文件
}
InputStream fis=new FileInputStream(srcFile);
OutputStream fos=new FileOutputStream(encFile);
while((dataOfFile=fis.read())>-1) {//当读到文件内容时
fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入
}
fis.close();
fos.flush();
fos.close();
}
private static void DecFile(File encFile,File decFile)throws Exception{
if(!encFile.exists()) {
System.out.println("encrypt file not exixt");
}
if(!decFile.exists()) {
System.out.println("decrypt file created");
decFile.createNewFile();
}
InputStream fis=new FileInputStream(encFile);
OutputStream fos=new FileOutputStream(decFile);
while((dataOfFile=fis.read())>-1) {
fos.write(dataOfFile^numOfEncAndDec);
}
fis.close();
fos.flush();
fos.close();
}
File srcFile=new File("D:\\JAVAfiles\\X1.txt");//初始化文件
File encFile=new File("D:\\JAVAfiles\\X2.txt"); //加密文件
File decFile=new File("D:\\JAVAfiles\\X3.txt"); //解密文件
try {
EncFile(srcFile,encFile); //加密操作
DecFile(encFile,decFile);
}catch(Exception e) {
e.printStackTrace();
}
}
private static void EncFile(File srcFile,File encFile)throws Exception{
if(!srcFile.exists()) {
System.out.println("source file not exixt");
}
if(!encFile.exists()) {
System.out.println("encrypt file created");
encFile.createNewFile();//若无加密文件,新建一个加密文件
}
InputStream fis=new FileInputStream(srcFile);
OutputStream fos=new FileOutputStream(encFile);
while((dataOfFile=fis.read())>-1) {//当读到文件内容时
fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入
}
fis.close();
fos.flush();
fos.close();
}
private static void DecFile(File encFile,File decFile)throws Exception{
if(!encFile.exists()) {
System.out.println("encrypt file not exixt");
}
if(!decFile.exists()) {
System.out.println("decrypt file created");
decFile.createNewFile();
}
InputStream fis=new FileInputStream(encFile);
OutputStream fos=new FileOutputStream(decFile);
while((dataOfFile=fis.read())>-1) {
fos.write(dataOfFile^numOfEncAndDec);
}
fis.close();
fos.flush();
fos.close();
}
}
![](https://img2018.cnblogs.com/blog/1279396/201811/1279396-20181121093437064-992789975.png)