小编给大家分享一下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);
}
以上是“Java常见IO面试题有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/69977571/viewspace-2707611/