[置顶] think in java interview-高级开发人员面试宝典(八)
面经出了7套,收到许多读者的Email,有许多人说了,这些基础知识是不是为了后面进一步的”通向架构师的道路“做准备的?
对的,你们没有猜错,就是这样的,我一直在酝酿后面的”通向架构师的道路“如何开章。
说实话,我已经在肚子里准备好的后面的”通向架构师的道路“的内容自己觉得如果一下子全拿出来的话,很多人吃不消,因为架构越来越复杂,用到的知识越来越多,而且很多都是各知识点的混合应用。
所以,先以这几套面经来铺路,我们把基础打实了,才能把大楼造的更好。因为,一个架构师首先他是一个程序员,他的基础知识必须非常的扎实,API对于架构师来说已经不太需要eclipse的code insight(即在eclipse编辑器里打一个小点点就可以得到后面的函数),尤其是一些常用的JAVA API来说,是必须熟记于心的。
下面我们继续来几天面经,顺带便复习一下JAVA和数据库的一些基础。
Java IO流的复习
大家平时J2EE写多了,JAVA的IO操作可能都已经生疏了,面试时如果来上这么几道,是不是有点”其实这个问题很简单,可是我就是想不起来“的感觉啊?
呵呵!
JAVA的IO操作太多,我这边挑腾迅,盛大和百度的几道面试题,并整理出答案来供大家参考。
InputFromConsole
这个最简单不过了,从console接受用户输入的字符,如和用户有交互的命令行。
如果你不复习的话,嘿嘿,还真答不出,来看:
package org.sky.io; public class InputFromConsole { /**
* @param args
*/
public static void main(String[] args) throws Exception {
int a = 0;
byte[] input = new byte[1024];
System.in.read(input);
System.out.println("your input is: " + new String(input)); } }
ListDir
列出给出路径下所有的目录,包括子目录
package org.sky.io; import java.io.*; public class ListMyDir { /**
* @param args
*/
public static void main(String[] args) {
String fileName = "D:" + File.separator + "tomcat2";
File f = new File(fileName);
File[] fs = f.listFiles();
for (int i = 0; i < fs.length; i++) {
System.out.println(fs[i].getName());
} } }
咦,上面这个程序只列出了一层目录,我们想连子目录一起List出来怎么办?
ListMyDirWithSubDir
package org.sky.io; import java.io.*; public class ListMyDirWithSubDir { /**
* @param args
*/
public void print(File f) {
if (f != null) {
if (f.isDirectory()) {
File[] fileArray = f.listFiles();
if (fileArray != null) {
for (int i = 0; i < fileArray.length; i++) {
print(fileArray[i]);
}
}
} else {
System.out.println(f);
}
}
} public static void main(String[] args) {
String fileName = "D:" + File.separator + "tomcat2";
File f = new File(fileName);
ListMyDirWithSubDir listDir = new ListMyDirWithSubDir();
listDir.print(f); }
}
InputStreamDemo
从外部读入一个文件
package org.sky.io; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; public class InputStreamDemo {
public void readFile(String fileName) {
File srcFile = new File(fileName);
InputStream in = null;
try {
in = new FileInputStream(srcFile);
byte[] b = new byte[(int) srcFile.length()];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) in.read();
}
System.out.println(new String(b));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
in = null;
}
} catch (Exception e) {
}
}
} public static void main(String[] args) {
InputStreamDemo id = new InputStreamDemo();
String src = "D:" + File.separator + "hello.txt";
id.readFile(src);
} }
OutputStreamDemo
讲完了InputStream来讲OutputStream,输出内容至外部的一个文件
package org.sky.io; import java.io.*; public class OutputStreamDemo { public void writeWithByte() {
String fileName = "D:" + File.separator + "hello.txt";
OutputStream out = null;
File f = new File(fileName);
try {
out = new FileOutputStream(f, true);
String str = " [Publicity ministry of ShangHai Municipal committee of CPC]";
byte[] b = str.getBytes();
out.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
out = null;
}
} catch (Exception e) {
}
}
} public void writeWithByteArray() {
String fileName = "D:" + File.separator + "hello.txt";
OutputStream out = null;
File f = new File(fileName);
try {
out = new FileOutputStream(f, true);
String str = " [hello with byte yi ge ge xie]";
byte[] b = str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
out = null;
}
} catch (Exception e) {
}
}
} public static void main(String[] args) {
OutputStreamDemo od = new OutputStreamDemo();
od.writeWithByte();
od.writeWithByteArray(); } }
这个Demo里分别用了”writeWithByte“和 ”writeWithByteArray“两种方法,注意查看
CopyFile
我们讲完了InputStream和OutputStream,我们就可以自己实现一个File Copy的功能了,来看
package org.sky.io; import java.io.*; public class CopyFile { public void copy(String src, String des) {
File srcFile = new File(src);
File desFile = new File(des);
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(desFile);
byte[] b = new byte[(int) srcFile.length()];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) in.read();
}
out.write(b);
System.out.println("copied [" + srcFile.getName() + "] with "
+ srcFile.length());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
out = null;
}
} catch (Exception e) {
}
try {
if (in != null) {
in.close();
in = null;
}
} catch (Exception e) {
}
}
} public static void main(String[] args) {
CopyFile cp = new CopyFile();
String src = "D:" + File.separator + "UltraEdit.zip";
String des = "D:" + File.separator + "UltraEdit_Copy.zip";
long sTime = System.currentTimeMillis();
cp.copy(src, des);
long eTime = System.currentTimeMillis();
System.out.println("Total spend: " + (eTime - sTime));
} }
运行后显示:
来看我们被Copy的这个文件的大小:
也不大,怎么用了7秒多?
原是我们没有使用Buffer这个东西,即缓冲,性能会相差多大呢?来看
BufferInputStreamDemo
package org.sky.io; import java.io.*; public class BufferInputStreamDemo { /**
* @param args
*/
public void copy(String src, String des) {
File srcFile = new File(src);
File desFile = new File(des);
BufferedInputStream bin = null;
BufferedOutputStream bout = null;
try {
bin = new BufferedInputStream(new FileInputStream(srcFile));
bout = new BufferedOutputStream(new FileOutputStream(desFile));
byte[] b = new byte[1024];
while (bin.read(b) != -1) {
bout.write(b);
}
bout.flush();
System.out.println("copied [" + srcFile.getName() + "] with "
+ srcFile.length());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bout != null) {
bout.close();
bout = null;
}
} catch (Exception e) {
}
try {
if (bin != null) {
bin.close();
bin = null;
}
} catch (Exception e) {
}
}
} public static void main(String[] args) {
BufferInputStreamDemo bd = new BufferInputStreamDemo();
String src = "D:" + File.separator + "UltraEdit.zip";
String des = "D:" + File.separator + "UltraEdit_Copy.zip";
long sTime = System.currentTimeMillis();
bd.copy(src, des);
long eTime = System.currentTimeMillis();
System.out.println("Total spend: " + (eTime - sTime)); } }
我们Copy同样一个文件,用了多少时间呢?来看!
丫只用了14毫秒,CALL!!!
ByteArrayDemo
来看看使用ByteArray输出文件吧
package org.sky.io; import java.io.*; public class ByteArrayDemo { /**
* @param args
*/
public void testByteArray() {
String str = "HOLLYJESUS";
ByteArrayInputStream input = null;
ByteArrayOutputStream output = null;
try {
input = new ByteArrayInputStream(str.getBytes());
output = new ByteArrayOutputStream();
int temp = 0;
while ((temp = input.read()) != -1) {
char ch = (char) temp;
output.write(Character.toLowerCase(ch));
}
String outStr = output.toString();
input.close();
output.close();
System.out.println(outStr);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null) {
output.close();
output = null;
}
} catch (Exception e) {
}
try {
if (input != null) {
input.close();
input = null;
}
} catch (Exception e) {
}
}
} public static void main(String[] args) {
ByteArrayDemo bd = new ByteArrayDemo();
bd.testByteArray(); } }
RandomAccess
有种输出流叫Random,你们还记得吗?学习时记得的,工作久了,HOHO,忘了,它到底有什么特殊的地方呢?来看:
package org.sky.io; import java.io.*; public class RandomAccess {
public void writeToFile() {
String fileName = "D:" + File.separator + "hello.txt";
RandomAccessFile randomIO = null;
try { File f = new File(fileName);
randomIO = new RandomAccessFile(f, "rw");
randomIO.writeBytes("asdsad");
randomIO.writeInt(12);
randomIO.writeBoolean(true);
randomIO.writeChar('A');
randomIO.writeFloat(1.21f);
randomIO.writeDouble(12.123);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (randomIO != null) {
randomIO.close();
randomIO = null;
}
} catch (Exception e) {
}
}
} public static void main(String[] args) {
RandomAccess randomA = new RandomAccess();
randomA.writeToFile();
}
}
它输出后的文件是怎么样的呢?
PipeStream
这个流很特殊,我们在线程操作时,两个线程都在运行,这时通过发送一个指令让某个线程do something,我们在以前的jdk1.4中为了实现这样的功能,使用的就是这个PipeStream
先来看两个类,一个叫SendMessage,即发送一个指令。一个叫ReceiveMessage,用于接受指令。
SendMessage
package org.sky.io; import java.io.*; public class SendMessage implements Runnable {
private PipedOutputStream out = null; public PipedOutputStream getOut() {
return this.out;
} public SendMessage() {
this.out = new PipedOutputStream();
} public void send() { String msg = "start";
try {
out.write(msg.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
out = null;
}
} catch (Exception e) {
}
}
} public void run() {
try {
System.out.println("waiting for signal...");
Thread.sleep(2000);
send();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ReceiveMessage
package org.sky.io; import java.io.*; public class ReceiveMessage implements Runnable {
private PipedInputStream input = null; public PipedInputStream getInput() {
return this.input;
} public ReceiveMessage() {
this.input = new PipedInputStream();
} private void receive() { byte[] b = new byte[1000];
int len = 0;
String msg = "";
try {
len = input.read(b);
msg = new String(b, 0, len);
if (msg.equals("start")) {
System.out
.println("received the start message, receive now can do something......");
Thread.interrupted();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
input = null;
}
} catch (Exception e) {
}
}
} public void run() {
try {
receive();
} catch (Exception e) {
}
}
}
如何使用这两个类呢?
TestPipeStream
package org.sky.io; public class TestPipeStream { /**
* @param args
*/
public static void main(String[] args) {
SendMessage send = new SendMessage();
ReceiveMessage receive = new ReceiveMessage();
try {
send.getOut().connect(receive.getInput());
Thread t1 = new Thread(send);
Thread t2 = new Thread(receive);
t1.start();
t2.start(); } catch (Exception e) {
e.printStackTrace();
} } }
注意这边有一个send.getOut().connect(receive.getInput());
这个方法就把两个线程”connect“起来了。
Serializable的IO操作
把一个类序列化到磁盘上,怎么做?
先来看我们要序列化的一个Java Bean
Person
package org.sky.io; import java.io.Serializable; public class Person implements Serializable { private String name = "";
private String age = "";
private String personId = ""; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getPersonId() {
return personId;
} public void setPersonId(String personId) {
this.personId = personId;
} public String getCellPhoneNo() {
return cellPhoneNo;
} public void setCellPhoneNo(String cellPhoneNo) {
this.cellPhoneNo = cellPhoneNo;
} private String cellPhoneNo = "";
}
下面来看序列化的操作
SerializablePersonToFile
package org.sky.io; import java.io.*;
import java.util.*; public class SerializablePersonToFile { /**
* @param args
*/
private List<Person> initList() {
List<Person> userList = new ArrayList<Person>();
Person loginUser = new Person();
loginUser.setName("sam");
loginUser.setAge("30");
loginUser.setCellPhoneNo("13333333333");
loginUser.setPersonId("111111111111111111");
userList.add(loginUser);
loginUser = new Person();
loginUser.setName("tonny");
loginUser.setAge("31");
loginUser.setCellPhoneNo("14333333333");
loginUser.setPersonId("111111111111111111");
userList.add(loginUser);
loginUser = new Person();
loginUser.setName("jim");
loginUser.setAge("28");
loginUser.setCellPhoneNo("15333333333");
loginUser.setPersonId("111111111111111111");
userList.add(loginUser);
loginUser = new Person();
loginUser.setName("Simon");
loginUser.setAge("30");
loginUser.setCellPhoneNo("17333333333");
loginUser.setPersonId("111111111111111111");
userList.add(loginUser);
return userList;
} private void serializeFromFile() {
FileInputStream fs = null;
ObjectInputStream ois = null;
try {
fs = new FileInputStream("person.txt");
ois = new ObjectInputStream(fs);
List<Person> userList = (ArrayList<Person>) ois.readObject();
for (Person p : userList) {
System.out.println(p.getName() + " " + p.getAge() + " "
+ p.getCellPhoneNo() + " " + p.getCellPhoneNo());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
} catch (Exception e) {
}
try {
if (fs != null) {
fs.close();
}
} catch (Exception e) {
}
}
} private void serializeToFile() {
List<Person> userList = new ArrayList<Person>();
userList = initList();
FileOutputStream fs = null;
ObjectOutputStream os = null;
try {
fs = new FileOutputStream("person.txt");
os = new ObjectOutputStream(fs);
os.writeObject(userList);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
} catch (Exception e) {
}
try {
if (fs != null) {
fs.close();
}
} catch (Exception e) {
}
}
} public static void main(String[] args) {
SerializablePersonToFile sf = new SerializablePersonToFile();
sf.serializeToFile();
sf.serializeFromFile();
} }
这边先把Person输出到Person.txt,再从Person.txt里反序列化出这个Person的Java Bean。
先讲这么些吧!
Java的流操作还有很多,这些是经常会被面试到的,很基础,因此经常被考到。
以前听一个读IT的同学说过,这些IO操作,就算没有Eclipse编辑器的话,用文本编辑器也应该能够写出来,你写不出只能代表你的基础太弱了。
[置顶] think in java interview-高级开发人员面试宝典(八)的更多相关文章
- [置顶] think in java interview-高级开发人员面试宝典(二)
从现在开始,以样题的方式一一列出各种面试题以及点评,考虑到我在前文中说的,对于一些大型的外资型公司,你将会面临全程英语面试,因此我在文章中也会出现许多全英语样题. 这些题目来自于各个真实的公司,公司名 ...
- [置顶] think in java interview-高级开发人员面试宝典(一)
“生死六重门” 无论你是在职,非在职,高级工程师,工程师,架构师,如果你正在面试阶段,请看完此文! 相信这篇文章对你的职业生涯和阶值观会造成重大的改变! 如果你是一名PM或者是管理者正在物色合适的开发 ...
- [置顶] think in java interview-高级开发人员面试宝典代码示例
下载资源地址为: http://download.csdn.net/detail/lifetragedy/6379755 这是think in java interview中的代码示例,包括JAVA基 ...
- [置顶] think in java interview-高级开发人员面试宝典(三)
收集自Oracle公司的10次(60道)电话面试全部问答(英语) Q: What environment variables do I need to set on my machine in ord ...
- [置顶] think in java interview-高级开发人员面试宝典(七)
上两周研发任务太紧了,所以担搁了一下,我们继续我们的面试之旅. 下面是一个基于图书系统的15道SQL问答,供大家参考 问题描述:本题用到下面三个关系表:CARD 借书卡. CNO 卡号,N ...
- think in java interview-高级开发人员面试宝典(一)
"生死六重门" 无论你是在职,非在职,高级工程师,工程师,架构师,如果你正在面试阶段,请看完此文! 相信这篇文章对你的职业生涯和阶值观会造成重大的改变! 如果你是一名PM或者是管理 ...
- [置顶] think in java interview番外篇-谈程序员如何修练英语
一.程序员对英语能力的重视度和能力要求应该是在各行各业中排在比较靠前的 这样说吧,英语程度的好坏直接影响着一个程序员的编程.开发.创新能力. 道理很简单: 1. 计算机和软件是用英语创造出来的 2. ...
- .NET方向高级开发人员面试时应该事先考虑的问题
(澄清一下,我发帖的目的不是用来专门给人评价这些问题的好坏的,实际上我所在 的公司就考察这些问题.而能够基本上答出来的可以说百里挑一.如果各位觉得这些问题很简单,那么基本上在.NET面试环节就没有什么 ...
- Java高级开发工程师面试笔记
最近在复习面试相关的知识点,然后做笔记,后期(大概在2018.02.01)会分享给大家,尽自己最大的努力做到最好,还希望到时候大家能给予建议和补充 ----------------2018.03.05 ...
随机推荐
- js去除首尾空格
简单的:str = jQuery.trim(str); var temp = " aa b "; console.log("cc" + temp); temp ...
- JQuery隔行变色
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- MyEclipse中spring MVC的配置
---恢复内容开始--- web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app ...
- 我的Python成长之路---第八天---Python基础(24)---2016年3月5日(晴)
多线程编程 什么是多线程,线程是操作系统能够进行运算调度的最小单位.他包含在进程之中,是进程中的实际运作单位.线程是进程中一个单顺序的空值六,一个进程可以并发多个线程,每个线程可以并行处理不同的任务. ...
- git 代理设置
git 代理设置: git config --global http.proxy http://proxy.com:8080git config --global https.proxy http:/ ...
- 一个ShellExecute的超全说明(www.phidels.com这个网站很多内容)
I. Introduction / Déclaration ShellExecute fait partie des innombrables fonctions de l'API Windows ; ...
- 引用 字库编码Unicode相关知识
引用 weifeng.shen 的 字库编码Unicode相关知识 1. 各地编码 首先说明一下现在常用的一些编码方案: 1. 在中国,大陆最常用的就是GBK18030编码, ...
- python类继承
面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...
- 为了肾六(dp)
为了肾六 时间限制:4000 ms | 内存限制:210535 KB 难度:2 描述 最近肾六很流行,goshawk看身边的朋友都用上了apple.自己还用着W年前的Samsung.于是决定去 ...
- 饭卡------HDOJ杭电2546(还是01背包!!!!!!)
Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩 ...