java的io面试题_【Java面试】Java常见IO面试题!

1:什么是流?

字符串分解==============OutStream==========>write()方法写到文件中

2:描述I/O流的基本接口和类的结构

InputStream

OutputStream

3:代码示例:如何使用URL流来进行输入输出

try {

imageSource = new URL("http://mysite.com/~info");

} catch (MalformedURLException e) {

}

4:什么是Unicode?

是一种字符的编码方式

5:代码示例:如何使用Reader和Writer来进行输入输出

InputStreamReader ir = new InputStreamReader(System.in);

OutStreamReader or = new OutStreamReader(System.in);

6:什么是可序列化?如何实现可序列化?

表示一个数据可以按流式输出

实现java.io.Serializable接口

7:代码示例:如何读写对象流

//读

try {

String str = "123";

FileOutputStream f = new FileOutputStream("test.txt");

ObjectOutputStream s = new ObjectOutputStream(f);

s.writeObject(str);

f.close();

}catch(Exception e) {

e.printStackTrace();

}

//写

try {

FileInputStream f = new FileInputStream("test.txt");

ObjectInputStream s = new ObjectInputStream(f);

str =(String)s.readObject();

f.close();

}catch(Exception e){

e.printStackTrace();

}

8:简述File类的基本功能

处理文件和获取文件信息,文件或文件夹的管理

除了读写文件内容其他的都可以做

9:代码示例:如何使用随机文件读写类来读写文件内容

RW表示文件时可读写的

读:

try{

RandomAccessFile f = new RandomAccessFile("test.txt", "rw");

long len = 0L;

long allLen = f.length();

int i = 0;

while (len < allLen) {

String s = f.readLine();

if (i > 0) {

col.add(s);

}

i++;

//游标

len = f.getFilePointer();

}

}catch(Exception err){

err.printStackTrace();

}

写:

try{

RandomAccessFile f = new RandomAccessFile("test.txt", "rw");

StringBuffer buffer = new StringBuffer("\n");

Iterator it = col.iterator();

while (it.hasNext()) {

buffer.append(it.next() + "\n");

}

f.writeUTF(buffer.toString());

}catch(Exception err){

err.printStackTrace();

}

10:代码示例:如何使用流的基本接口来读写文件内容

try{

DataInputStream in =

new DataInputStream(

new BufferedInputStream(

new FileInputStream("Test.java")

)

);

while ((currentLine = in.readLine()) != null){

System.out.println(currentLine);

}

}catch (IOException e){

System.err.println("Error: " + e);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值