一.以字节的方式读取和写入
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
二.文件的读取
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
*
* @author ghh
*/
public class ScanFile {
public void scanFiles(File file) {
try {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
scanFiles(new File(files[i].getAbsolutePath()));
String path = files[i].getAbsolutePath();
int index = path.indexOf("mirror");
path = path.substring(index + 7).trim();
System.out.println("path " + path);
File f = new File("/home/zdl/fw.txt");
this.readFile(f, path);
}
}
} catch (Exception e) {
}
}
public void readFile(File fw, String path) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fw)));
String data = null;
String str = null;
while ((data = br.readLine()) != null) {
str = "http://" + path + "/" + data.trim();
this.write(str);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void write(String path) {
try {
File ganji = new File("/home/zdl/ganji.lst");
if (!ganji.isFile()) {
ganji.createNewFile();
}
FileWriter fw = new FileWriter(ganji, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(path);
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public void write1(File file) {
try {
File[] files = file.listFiles();
File ganji = new File("/home/zdl/ganji.lst");
FileWriter fw = new FileWriter(ganji, true);
PrintWriter pw = new PrintWriter(fw);
if (!ganji.isFile()) {
ganji.createNewFile();
}
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
write1(new File(files[i].getAbsolutePath()));
} else {
String path = files[i].getAbsolutePath();
int index = path.indexOf("mirror");
path ="http://" + path.substring(index + 7, path.length() - 11).trim();
System.out.println("path " + path);
pw.println(path);
}
}
pw.flush();
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getCity(File file) {
File[] files = file.listFiles();
System.out.println("size "+files.length);
for(int i = 0; i<files.length; i++) {
if(files[i].isDirectory()) {
System.out.println(files[i].getName());
}
}
}
public static void main(String[] args) {
ScanFile s = new ScanFile();
File file = new File("/home/zdl/Downloads/mirror");
// File f = new File("/home/zdl/fw.txt");
// s.scanFile(file);
// s.readFile(f);
// s.write1(file);
s.getCity(file);
}
}
三.读文件
bufferedReader br = new bufferedReader(new FileReader(file))
四.写入文件
printwriter pw = new PrintWriter(new FileoutputStream(file))