读数据:
方法一:
import java
.io
.FileInputStream
;
import java
.io
.FileNotFoundException
;
import java
.io
.IOException
;
public class FileInputStreamDeam {
public static void main(String
[] args
) throws IOException
{
FileInputStream fis
= new FileInputStream("a.txt");
int by
;
while ((by
= fis
.read())!= -1){
System
.out
.print((char)by
);
}
fis
.close();
}
}
方法二:
import java
.io
.FileInputStream
;
import java
.io
.FileNotFoundException
;
import java
.io
.IOException
;
public class FileInputStreamDeam02 {
public static void main(String
[] args
) throws IOException
{
FileInputStream fis
= new FileInputStream("b.txt");
byte[] bys
= new byte[1024];
int len
;
while ((len
= fis
.read(bys
))!= -1){
System
.out
.print(new String(bys
, 0 ,len
));
}
fis
.close();
}
}
写出数据:
import java
.io
.FileOutputStream
;
import java
.io
.IOException
;
public class StreamDemo {
public static void main(String
[] args
) throws IOException
{
FileOutputStream fos
= new FileOutputStream("a.txt");
String s
= "avsjfhafkfnf";
byte[] bytes
= s
.getBytes();
fos
.write(bytes
);
fos
.close();
}
}
转载请注明原文地址: https://yun.8miu.com/read-62257.html