stream2String 文件遍历
public class IOStreamDemo {
public static void isFileExist(String path, String filename) {
File file = new File(path + filename);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void isFilesExist(String path, String filename) {
File file = new File(path + filename);
if (!file.exists()) {
file.mkdir();
}
}
public static String stream2String(InputStream inputStream) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int length = 0;
byte[] buffer = new byte[1024];
while ((length = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
return baos.toString();
} catch (IOException e) {
e.printStackTrace();
try {
baos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return null;
}
public static String streamToString(InputStream inputStream) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
br.close();
return stringBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return null;
}
public static void copyFile(File filePath, File file) {
try {
if (filePath.isDirectory() && file.isFile()) {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String st = filePath.getPath().replace("\\", "/") + "/" + file.getName();
PrintWriter pw = new PrintWriter(st);
String length = "";
while ((length = br.readLine()) != null) {
pw.println(length);
pw.flush();
}
pw.close();
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void fileCopy(File sourfile, File targetfile, String name) {
try {
if (sourfile != null && sourfile.isFile()) {
FileInputStream fis = new FileInputStream(sourfile);
FileOutputStream fos = new FileOutputStream(targetfile.getPath().replace("\\", "/") + "/" + name);
int count = 0;
byte[] b = new byte[1024];
while ((count = fis.read(b)) > 0) {
fos.write(b, 0, count);
fos.flush();
}
fos.close();
fis.close();
} else if (sourfile == null) {
File file = new File(targetfile.getPath().replace("\\", "/") + "/" + name);
file.createNewFile();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void fileFind(File sourfile, String name, List<File> list) {
if (sourfile.isDirectory() && list != null) {
File[] files = sourfile.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
fileFind(file, name, list);
} else if (file.isFile()) {
if (file.getName().equals(name)) {
list.add(file);
}
}
}
}
}
}
public static void search(File sourFile, String endName, List<File> list) {
File[] files = sourFile.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
search(file, endName, list);
} else if (file.isFile()) {
if (file.getName().endsWith(endName)) {
list.add(file);
}
}
}
}
}
public static List<String> getFileLength(List<File> list) {
List<String> fileName = new ArrayList<String>();
if (list != null && list.size() > 0) {
for (File file : list) {
fileName.add("文件名为" + file + "大小为" + file.length() / 1024 / 1024 + "兆");
}
}
return fileName;
}
public static void ListFileFileter(File dir, final String endName) {
if (dir.exists()) {
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
String name = pathname.getName();
System.out.println("****************" + pathname);
return name.endsWith(endName) ;
}
});
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
files[i].delete();
} else {
ListFileFileter(files[i],endName);
}
}
} else {
throw new RuntimeException("操作的文件或者目录不存在!");
}
}
}