Javaバイト入出力ストリーム
1154 ワード
簡単に小例を書いた
package edu.java.io;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* @author Guo
*
* 2016-2-4
*/
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write(97);//a
fos.write("ppleo".getBytes(), 0, 4);//pple ,
fos.close();
}
}
package edu.java.io;
import java.io.FileInputStream;
import java.io.IOException;
/**
*
* @author Guo
*
* 2016-2-4
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("fos.txt");
//
int b = 0;
while((b=fis.read()) != -1){
System.out.print((char)b);//apple
}
// ( : , )
byte[] bys = new byte[1024];
int len = 0;
while((len=fis.read(bys)) != -1){
System.out.println(new String(bys,0,len));// String.valueOf(), vlaueOf(byte[] data),
}
fis.close();
}
}