- java文件IO流基本操作
package PackageOfJava01;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale.Category;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
public class PracticeOfStream {
public static void main(String[] args) {
File myFiles=new File("myTest.txt");
OutputStream is=null;
String myString="niit.ruanjian.zhuzhile";
try {
is=new FileOutputStream(myFiles,true);
byte[] datas =myString.getBytes();
is.write(datas,0,datas.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e)
{
e.getMessage();
}
}
}
package PackageOfJava01;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale.Category;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
public class PracticeOfStream {
public static void main(String[] args) {
File myFiles=new File("myTest.txt");
try {
InputStream in=new FileInputStream(myFiles);
byte[] car=new byte[3];
int len=-1;
while((len=in.read(car))!=-1)
{
String str= new String(car, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
总结:一共分四步,一步是创建源,一步是创建流,一步是执行read和wirte操作,一步使close操作;
read和wirte操作,有三种read(),read(bytes[]),read(bytes[],offset,len);
wirte操作也如是,还有byte[]与string的转换,string向byte转换,使用getbyte(),byte[]向string转换,string(bytes[],offset,len);