java中io流的使用例子
2010-12-09 10:45
330 查看
import java.io.bufferedreader
import java.io.bufferedwriter
import java.io.bytearrayinputstream
import java.io.bytearrayoutputstream
import java.io.datainputstream
import java.io.dataoutputstream
import java.io.file
import java.io.fileinputstream
import java.io.filenotfoundexception
import java.io.fileoutputstream
import java.io.filereader
import java.io.filewriter
import java.io.ioexception
import java.io.inputstream
import java.io.inputstreamreader
import java.io.linenumberreader
import java.io.objectinputstream
import java.io.objectoutputstream
import java.io.outputstream
import java.io.outputstreamwriter
import java.io.printstream
import java.io.printwriter
import java.io.randomaccessfile
import java.io.serializable
import java.io.stringreader
import java.io.stringwriter
import java.nio.bytebuffer
import java.nio.channels.filechannel
class person implements serializable{
int age
string name
transient string password
public person(int age string name string password) {
super()
this.age = age
this.name = name
this.password = password
}
}
public class testinputstream {
public static void input
(){
try {
fileinputstream fis=new fileinputstream(" d://1//1.txt" )
int i=0
/while((i=fis.read())!=-1)
{
system.out.print((char)i)
}/
byte[] bt=new byte[130]
fis.read(bt)
system.out.println(new string(bt " gbk" ).trim())
fis.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void output
(){
try {
fileoutputstream fis=new fileoutputstream(" d://1//2.txt" )
fis.write(" 我爱大连理工" .getbytes())
fis.flush()
fis.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filereadertest
(){
try {
filereader fr=new filereader(" d://1//1.txt" )
int c
/while((c=fr.read())!=-1)
{
system.out.print((char)c)
}/
char[] ch=new char[100]
fr.read(ch)
system.out.println(new string(ch).trim())
fr.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filewritertest
(){
try {
filewriter fw=new filewriter(" d://1//2.txt" )
fw.write(" 我爱大连理工大uxe" )
fw.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void bufferedreadertest
(
){
try {
bufferedreader br=new bufferedreader(new filereader(" d://1//1.txt" ))
string str=" "
while((str=br.readline())!=null)
{
system.out.println(str)
}
br.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void bufferedwritertest
(){
try {
bufferedwriter br=new bufferedwriter(new filewriter(" d://1//2.txt" ))
br.write(" 我爱大连理工大uxec" )
br.write(123)
br.write(" 大量咯规范化" .tochararray())
br.flush()
br.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void inputstreamreader
(){
try {
inputstreamreader isr=new inputstreamreader(new fileinputstream(" d://1//1.txt" ))
int i=0
/while((i=isr.read())!=-1)
{
system.out.print((char)i)
}/
char[] ch=new char[40]
isr.read(ch)
system.out.println(ch)
isr.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void outputstreamwriter
(){
try {
outputstreamwriter isw=new outputstreamwriter(new fileoutputstream(" d://1//2.txt" ))
isw.write(" 使得法国就回家后即可大富豪国家会计科" )
isw.write(" safhghgj" .tochararray())
isw.flush()
isw.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void dataoutputstreamtest
(){
try {
dataoutputstream dos=new dataoutputstream(new fileoutputstream(" d://1//1.txt" ))
dos.writeutf(" 我爱大连理工大uxe" )
dos.writedouble(12345)
dos.flush()
dos.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void datainputstreamtest
(){
try {
datainputstream dis=new datainputstream(new fileinputstream(" d://1//1.txt" ))
string str=dis.readutf()
double d=dis.readdouble()
system.out.println(str)
system.out.println(" double" +d)
dis.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void bytearraystream
(){
byte[] bt=" 大连理工大学" .getbytes()
bytearrayoutputstream bos=new bytearrayoutputstream()
dataoutputstream dos=new dataoutputstream(bos)
try {
dos.writeutf(" 大连理工度邪恶" )
dos.writelong(23145l)
dos.flush()
dos.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
//如下读取会生成错误
/byte[] b=bos.tobytearray()
try {
system.out.println(new string(b " gbk" ))
} catch (unsupportedencodingexception e) {
// todo auto-generated catch block
e.printstacktrace()
}/
bytearrayinputstream bis=new bytearrayinputstream(bos.tobytearray())
datainputstream dis=new datainputstream(bis)
try {
string str=dis.readutf()
long l=dis.readlong()
system.out.println(str)
system.out.println(l)
dis.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void objectoutputstreamtest
(){
try {
objectoutputstream oos=new objectoutputstream(new fileoutputstream(" d://1//2.txt" ))
person p=new person(4 " we" " ertr" )
oos.writeobject(p)
person p1=new person(34 " dfhghgfhgfhj" " fhgkjhlkjp lk lk ' " )
oos.writeobject(p1)
oos.flush()
oos.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void objectinputstreamtest
(){
try {
objectinputstream ois=new objectinputstream(new fileinputstream(" d://1//2.txt" ))
person p=(person)ois.readobject()
person p2=(person)ois.readobject()
string str1=p.name
string str2=p2.name
/
由于password设置为transient 就是不能序列化,不会被存储,也不能被读取
/
string pass1=p.password
string pass2=p2.password
system.out.println(" 用户1:" +str1+" 密码" +pass1)
system.out.println(" 用户2:" +str2+" 密码" +pass2)
ois.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void systeminputstreamreader
(){
//使用fileinputstream不行
inputstreamreader fis=new inputstreamreader(system.in)
int i=0
try {
/ while((i=fis.read())!=-1)
{
system.out.print((char)i)
}/
char[] ch=new char[35]
//显示多余数据
fis.read(ch)
system.out.print(ch)
fis.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void systeminputstream
(){
//使用fileinputstream不行
inputstream is=system.in
byte[] bt=new byte[35]
try {
is.read(bt)
system.out.println(new string(bt).trim())
is.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void systemoutputstream
(){
//使用fileinputstream不行
outputstream os=system.out
try {
os.write(" 按时打发过来" .getbytes())
os.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void testshurushuchuzhuanghuan
(){
try {
printstream console=system.out
fileinputstream fis=new fileinputstream(" d://1//1.txt" )
system.setin(fis)
fileoutputstream fos=new fileoutputstream(" d://1//2.txt" )
printstream ps=new printstream(fos)
system.setout(ps)
system.seterr(ps)
bufferedreader br=new bufferedreader(new inputstreamreader(system.in))
string str=" "
while((str=br.readline())!=null)
{
system.out.println(str)
}
system.setout(console)
br.close()
fis.close()
fos.flush()
fos.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void randomaccessfileouttest
(){
try {
randomaccessfile raf=new randomaccessfile(new file(" d://1//1.txt" ) " rw" )
try {
raf.writeboolean(true)
raf.writedouble(234)
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
raf.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void randomaccessfileintest
(){
try {
randomaccessfile raf=new randomaccessfile(new file(" d://1//1.txt" ) " rw" )
try {
/
boolean占一个字节 向前跳1个字节
/
raf.seek(1)
//boolean t=raf.readboolean()
double d=raf.readdouble()
system.out.println(" double" +d)
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
raf.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filechanelouttest
(){
try {
filechannel fc=new fileoutputstream(" d://1//1.txt" ).getchannel()
fc.write(bytebuffer.wrap(" 大连理工大学李宁" .getbytes()))
fc.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filechanelgbktest(
){
try {
filechannel fc=new fileoutputstream(" d://1//1.txt" ).getchannel()
fc.write(bytebuffer.wrap(" 大连理工大学李宁" .getbytes(" utf-16be" )))
fc.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filechanelintest
(){
bytebuffer bf=bytebuffer.allocate(40)
try {
filechannel fc=new fileinputstream(" d://1//1.txt" ).getchannel()
fc.read(bf)
bf.flip()
/while(bf.hasremaining())
{
system.out.println((char)bf.get())
}/
/
以下仅适用于英文
/
system.out.println(bf.ascharbuffer())
//修改后
//string encoding=system.getproperty(" file.encoding" )
//system.out.println(charset.forname(encoding).decode(bf))
/
或者在输入时使用utf-16be
/
bf.clear()
fc.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void linenumberreadertest
(){
try {
linenumberreader lr=new linenumberreader(new filereader(" d://1//1.txt" ))
string str=" "
while((str=lr.readline())!=null)
{
system.out.println(lr.getlinenumber()+" " +str)
}
lr.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void printstreamtest
(){
try {
/
可以带file,也可以不带
/
//printstream ps=new printstream(new file(" d://1//1.txt" ))
printstream ps=new printstream(" d://1//1.txt" )
printstream console=system.out
system.setout(ps)
system.out.println(" 大连理工大些下次的法规和价格和" )
system.setout(console)
ps.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void printwritertest
(){
try {
/
printwriter()中可以是文件路径及文件名,也可以是file,也可以是outputstream对象
/
printwriter pw=new printwriter(" d://1//1.txt" )
pw.println(" 大利埃法规和加快了立刻离开" )
pw.write(" 是大方过后就看见" +" /n" )
pw.write(34)
pw.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void stringreadertest
(){
/
filereader不行
/
bufferedreader br=new bufferedreader(new stringreader(" 发的规范及会计幅度高于韩国进口" ))
try {
system.out.println(br.readline())
br.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void stringwritertest
(){
/
filereader不行
/
bufferedwriter br=new bufferedwriter(new stringwriter(123))
try {
br.write(" 按时大法官采购机构客户及客户即可" )
br.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void main(string[] args) {
//input()
//output()
//filereadertest()
//filewritertest()
//bufferedreadertest()
//bufferedwritertest()
//inputstreamreader()
//outputstreamwriter()
//dataoutputstreamtest()
//datainputstreamtest()
//bytearraystream()
//objectoutputstreamtest()
//objectinputstreamtest()
//systeminputstreamreader()
//systeminputstream()
//systemoutputstream()
//testshurushuchuzhuanghuan()
//randomaccessfileouttest()
//randomaccessfileintest()
//filechanelouttest()
//filechanelgbktest()
//filechanelintest()
//linenumberreadertest()
//printstreamtest()
//printwritertest()
//stringreadertest()
//stringwritertest()
}
}
import java.io.bufferedwriter
import java.io.bytearrayinputstream
import java.io.bytearrayoutputstream
import java.io.datainputstream
import java.io.dataoutputstream
import java.io.file
import java.io.fileinputstream
import java.io.filenotfoundexception
import java.io.fileoutputstream
import java.io.filereader
import java.io.filewriter
import java.io.ioexception
import java.io.inputstream
import java.io.inputstreamreader
import java.io.linenumberreader
import java.io.objectinputstream
import java.io.objectoutputstream
import java.io.outputstream
import java.io.outputstreamwriter
import java.io.printstream
import java.io.printwriter
import java.io.randomaccessfile
import java.io.serializable
import java.io.stringreader
import java.io.stringwriter
import java.nio.bytebuffer
import java.nio.channels.filechannel
class person implements serializable{
int age
string name
transient string password
public person(int age string name string password) {
super()
this.age = age
this.name = name
this.password = password
}
}
public class testinputstream {
public static void input
(){
try {
fileinputstream fis=new fileinputstream(" d://1//1.txt" )
int i=0
/while((i=fis.read())!=-1)
{
system.out.print((char)i)
}/
byte[] bt=new byte[130]
fis.read(bt)
system.out.println(new string(bt " gbk" ).trim())
fis.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void output
(){
try {
fileoutputstream fis=new fileoutputstream(" d://1//2.txt" )
fis.write(" 我爱大连理工" .getbytes())
fis.flush()
fis.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filereadertest
(){
try {
filereader fr=new filereader(" d://1//1.txt" )
int c
/while((c=fr.read())!=-1)
{
system.out.print((char)c)
}/
char[] ch=new char[100]
fr.read(ch)
system.out.println(new string(ch).trim())
fr.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filewritertest
(){
try {
filewriter fw=new filewriter(" d://1//2.txt" )
fw.write(" 我爱大连理工大uxe" )
fw.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void bufferedreadertest
(
){
try {
bufferedreader br=new bufferedreader(new filereader(" d://1//1.txt" ))
string str=" "
while((str=br.readline())!=null)
{
system.out.println(str)
}
br.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void bufferedwritertest
(){
try {
bufferedwriter br=new bufferedwriter(new filewriter(" d://1//2.txt" ))
br.write(" 我爱大连理工大uxec" )
br.write(123)
br.write(" 大量咯规范化" .tochararray())
br.flush()
br.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void inputstreamreader
(){
try {
inputstreamreader isr=new inputstreamreader(new fileinputstream(" d://1//1.txt" ))
int i=0
/while((i=isr.read())!=-1)
{
system.out.print((char)i)
}/
char[] ch=new char[40]
isr.read(ch)
system.out.println(ch)
isr.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void outputstreamwriter
(){
try {
outputstreamwriter isw=new outputstreamwriter(new fileoutputstream(" d://1//2.txt" ))
isw.write(" 使得法国就回家后即可大富豪国家会计科" )
isw.write(" safhghgj" .tochararray())
isw.flush()
isw.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void dataoutputstreamtest
(){
try {
dataoutputstream dos=new dataoutputstream(new fileoutputstream(" d://1//1.txt" ))
dos.writeutf(" 我爱大连理工大uxe" )
dos.writedouble(12345)
dos.flush()
dos.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void datainputstreamtest
(){
try {
datainputstream dis=new datainputstream(new fileinputstream(" d://1//1.txt" ))
string str=dis.readutf()
double d=dis.readdouble()
system.out.println(str)
system.out.println(" double" +d)
dis.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void bytearraystream
(){
byte[] bt=" 大连理工大学" .getbytes()
bytearrayoutputstream bos=new bytearrayoutputstream()
dataoutputstream dos=new dataoutputstream(bos)
try {
dos.writeutf(" 大连理工度邪恶" )
dos.writelong(23145l)
dos.flush()
dos.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
//如下读取会生成错误
/byte[] b=bos.tobytearray()
try {
system.out.println(new string(b " gbk" ))
} catch (unsupportedencodingexception e) {
// todo auto-generated catch block
e.printstacktrace()
}/
bytearrayinputstream bis=new bytearrayinputstream(bos.tobytearray())
datainputstream dis=new datainputstream(bis)
try {
string str=dis.readutf()
long l=dis.readlong()
system.out.println(str)
system.out.println(l)
dis.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void objectoutputstreamtest
(){
try {
objectoutputstream oos=new objectoutputstream(new fileoutputstream(" d://1//2.txt" ))
person p=new person(4 " we" " ertr" )
oos.writeobject(p)
person p1=new person(34 " dfhghgfhgfhj" " fhgkjhlkjp lk lk ' " )
oos.writeobject(p1)
oos.flush()
oos.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void objectinputstreamtest
(){
try {
objectinputstream ois=new objectinputstream(new fileinputstream(" d://1//2.txt" ))
person p=(person)ois.readobject()
person p2=(person)ois.readobject()
string str1=p.name
string str2=p2.name
/
由于password设置为transient 就是不能序列化,不会被存储,也不能被读取
/
string pass1=p.password
string pass2=p2.password
system.out.println(" 用户1:" +str1+" 密码" +pass1)
system.out.println(" 用户2:" +str2+" 密码" +pass2)
ois.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void systeminputstreamreader
(){
//使用fileinputstream不行
inputstreamreader fis=new inputstreamreader(system.in)
int i=0
try {
/ while((i=fis.read())!=-1)
{
system.out.print((char)i)
}/
char[] ch=new char[35]
//显示多余数据
fis.read(ch)
system.out.print(ch)
fis.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void systeminputstream
(){
//使用fileinputstream不行
inputstream is=system.in
byte[] bt=new byte[35]
try {
is.read(bt)
system.out.println(new string(bt).trim())
is.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void systemoutputstream
(){
//使用fileinputstream不行
outputstream os=system.out
try {
os.write(" 按时打发过来" .getbytes())
os.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void testshurushuchuzhuanghuan
(){
try {
printstream console=system.out
fileinputstream fis=new fileinputstream(" d://1//1.txt" )
system.setin(fis)
fileoutputstream fos=new fileoutputstream(" d://1//2.txt" )
printstream ps=new printstream(fos)
system.setout(ps)
system.seterr(ps)
bufferedreader br=new bufferedreader(new inputstreamreader(system.in))
string str=" "
while((str=br.readline())!=null)
{
system.out.println(str)
}
system.setout(console)
br.close()
fis.close()
fos.flush()
fos.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void randomaccessfileouttest
(){
try {
randomaccessfile raf=new randomaccessfile(new file(" d://1//1.txt" ) " rw" )
try {
raf.writeboolean(true)
raf.writedouble(234)
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
raf.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void randomaccessfileintest
(){
try {
randomaccessfile raf=new randomaccessfile(new file(" d://1//1.txt" ) " rw" )
try {
/
boolean占一个字节 向前跳1个字节
/
raf.seek(1)
//boolean t=raf.readboolean()
double d=raf.readdouble()
system.out.println(" double" +d)
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
raf.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filechanelouttest
(){
try {
filechannel fc=new fileoutputstream(" d://1//1.txt" ).getchannel()
fc.write(bytebuffer.wrap(" 大连理工大学李宁" .getbytes()))
fc.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filechanelgbktest(
){
try {
filechannel fc=new fileoutputstream(" d://1//1.txt" ).getchannel()
fc.write(bytebuffer.wrap(" 大连理工大学李宁" .getbytes(" utf-16be" )))
fc.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void filechanelintest
(){
bytebuffer bf=bytebuffer.allocate(40)
try {
filechannel fc=new fileinputstream(" d://1//1.txt" ).getchannel()
fc.read(bf)
bf.flip()
/while(bf.hasremaining())
{
system.out.println((char)bf.get())
}/
/
以下仅适用于英文
/
system.out.println(bf.ascharbuffer())
//修改后
//string encoding=system.getproperty(" file.encoding" )
//system.out.println(charset.forname(encoding).decode(bf))
/
或者在输入时使用utf-16be
/
bf.clear()
fc.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void linenumberreadertest
(){
try {
linenumberreader lr=new linenumberreader(new filereader(" d://1//1.txt" ))
string str=" "
while((str=lr.readline())!=null)
{
system.out.println(lr.getlinenumber()+" " +str)
}
lr.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void printstreamtest
(){
try {
/
可以带file,也可以不带
/
//printstream ps=new printstream(new file(" d://1//1.txt" ))
printstream ps=new printstream(" d://1//1.txt" )
printstream console=system.out
system.setout(ps)
system.out.println(" 大连理工大些下次的法规和价格和" )
system.setout(console)
ps.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void printwritertest
(){
try {
/
printwriter()中可以是文件路径及文件名,也可以是file,也可以是outputstream对象
/
printwriter pw=new printwriter(" d://1//1.txt" )
pw.println(" 大利埃法规和加快了立刻离开" )
pw.write(" 是大方过后就看见" +" /n" )
pw.write(34)
pw.close()
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void stringreadertest
(){
/
filereader不行
/
bufferedreader br=new bufferedreader(new stringreader(" 发的规范及会计幅度高于韩国进口" ))
try {
system.out.println(br.readline())
br.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void stringwritertest
(){
/
filereader不行
/
bufferedwriter br=new bufferedwriter(new stringwriter(123))
try {
br.write(" 按时大法官采购机构客户及客户即可" )
br.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
public static void main(string[] args) {
//input()
//output()
//filereadertest()
//filewritertest()
//bufferedreadertest()
//bufferedwritertest()
//inputstreamreader()
//outputstreamwriter()
//dataoutputstreamtest()
//datainputstreamtest()
//bytearraystream()
//objectoutputstreamtest()
//objectinputstreamtest()
//systeminputstreamreader()
//systeminputstream()
//systemoutputstream()
//testshurushuchuzhuanghuan()
//randomaccessfileouttest()
//randomaccessfileintest()
//filechanelouttest()
//filechanelgbktest()
//filechanelintest()
//linenumberreadertest()
//printstreamtest()
//printwritertest()
//stringreadertest()
//stringwritertest()
}
}
相关文章推荐
- Java中IO流的使用例子
- JAVA 中的IO流详解及其使用方法、例子
- JAVA 中的IO流详解及其使用方法、例子
- [bxd学习java基本点]9.ArrayList存对象例子,其除重复及删除,要使用地址,而不是eqauls
- Java日期格式化及其使用例子收集
- Java:IO流的一些典型例子
- Java回炉之IO流(二)使用规律
- Linux平台Java调用so库-JNI使用例子
- fasttext的基本使用 java 、python为例子
- java IO流的使用
- java序列化/反序列化之xstream、protobuf、protostuff 的比较与使用例子
- Java日期格式化及其使用例子收集
- javaIO 中JDK 1.1以后的IO流组合例子
- java序列化/反序列化之xstream、protobuf、protostuff 的比较与使用例子
- 一个使用JAVA xmlencoder 例子
- Linux平台Java调用so库-JNI使用例子
- [LUA]几个简单的luajava使用例子
- java 发送邮件 例子 (带附件的) 使用org.apache.commons.mail
- Junit4 -Java单元测试框架使用的例子
- java-监听器使用的例子