//ByteArrayInputStream
public static void main(String[] args) throws IOException {
String str = "zsy love jh !";
byte[] byteStr = str.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(byteStr);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = bais.read()) != -1) {
baos.write(c);
}
FileOutputStream os = new FileOutputStream("zsy.txt");
String o = baos.toString();
System.out.println(o);
baos.writeTo(os);
bais.close();
baos.close();
}
}
z这个例子比较好:
public static void main(String[] args) throws Exception {
// OutputStream os = new FileOutputStream("D:/jhh.txt");
// BufferedOutputStream bos = new BufferedOutputStream(os);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"D:/zsy.txt"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("jhh.txt"));
byte[] buffer = new byte[1024];
int length = 0;
while ((length = bis.read(buffer, 0, 1024)) != -1) {
bos.write(buffer, 0, length);
}
bis.close();
bos.close();
}
Finputstream
public static void main(String[] args) throws Exception {
//读
InputStream is = new FileInputStream("D:/zsy.txt");
//写
OutputStream os = new FileOutputStream("D:/jh.txt", true);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer, 0, 1024)) != -1) {
String str = new String(buffer, 0, length);
System.out.println(str);
os.write(buffer, 0, length);
}
is.close();
os.close();
}
BufferedReader reader = new BufferedReader(new FileReader(
"BufferedOutPutStreams.java"));
BufferedWriter writer = new BufferedWriter(
new FileWriter("t.txt", true));
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
writer.write(str + "\n");
}
reader.close();
writer.close();
}
private ByteArrayOutputStream getBaos(TJzxtRyRxzp tJzxtRyRxzp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (tJzxtRyRxzp != null) {
try {
InputStream in = new ByteArrayInputStream(tJzxtRyRxzp.getRxzp());
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(baos);
int i;
while ((i = bis.read()) != -1) {
bos.write(i);
}
bos.flush();
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return baos;
}