动力节点Java学院整理
Java面试题(11)请写一个程序的读写,要求用两种方式一种是低层流另一种是高层流。
import java.io.FileWriter;
import java.io.InputStream;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
public class Untitled1 {
public static void writeFileChar() throws Exception {
FileWriter f = new FileWriter("c:\\aa.txt");
InputStream is = System.in;
int c = is.read();
while (((char) c) != 'x') {
f.write(c);
c = is.read();
}
f.close();
is.close();
}
public static void writeFileString() throws Exception {
FileWriter f = new FileWriter("c:\\aa.txt");
BufferedWriter bwr=new BufferedWriter(f);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String c = bf.readLine();
while (!c.equals("stop")) {
bwr.write(c+"\r\n");
c = bf.readLine();
}
bwr.close();
f.close();
bf.close();
}
public static void readFileChar() throws Exception {
FileReader f = new FileReader("c:\\aa.txt");
int c = f.read();
while (c!=-1) {
System.out.print((char)c);
c=f.read();
}
f.close();
}
public static void readFileString() throws Exception {
BufferedReader bf = new BufferedReader(new FileReader("c:\\aa.txt"));
String c = bf.readLine();
while (c!=null)
{
System.out.println(c);
c=bf.readLine();
}
bf.close();
}
public static void main(String[] args) throws Exception {
readFileString();
}
}
Java面试题(12)如何列出某个目录下的所有文件
Import java.io.File;
File f=new File("C:\\");
File[] f1=f.listFiles();
for(int i=0;i<f1.length;i++)
{
if(f1[i].isDirectory())
{
System.out.println("dirctory is"+f1[i].getName());
}
else
{
System.out.println("file is"+f1[i].getName());
}
}
}
1. 如何列出某个目录下的所有子目录
public static void main(String[] args) throws Exception
{
getFile(new File("C:\\entityBean"),"\t");
}
public static void getFile(File f,String sem) throws Exception
{
System.out.println(sem+f.getName());
File fl[]=f.listFiles();
if(fl.length>=1)
{
for(int i=0;i<fl.length;i++)
{
if(fl[i].isDirectory())
{
getFile(fl[i],sem+"\t");
}
}
}
}
2. 判断一个文件或目录是否存在
File f=new File("C:\\entityBean");
if(f.exists())
{
System.out.println("exist");
}
else
{
System.out.println("not exist");
}
Socket
Java面试题(13)用socket 通讯写出客户端和服务器端的通讯,要求客户发送数据后能够回显相同的数据?
public class ServerSocket_1
{
public static void main(String[] args)
throws Exception
{
ServerSocket ss = new ServerSocket(4001);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.
getInputStream()));
PrintStream ps=new PrintStream(s.getOutputStream());
String temp = br.readLine();
while (true)
{
System.out.println("客户端:"+temp);
ps.println(temp);
if (temp.equals("stop"))
{
break;
}
temp = br.readLine();
}
br.close();
ps.close();
ss.close();
}
}
public class ClientSocket
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("localhost", 4001);
PrintStream ps = new PrintStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br_server = new BufferedReader(new InputStreamReader(s.
getInputStream()));
String temp = br.readLine();
while (true)
{
ps.println(temp);
temp = br_server.readLine();
System.out.println("服务器的信息:" + temp);
if (temp.equals("stop"))
{
break;
}
temp = br.readLine();
}
s.close();
br.close();
br_server.close();
}
}
Java面试题(14)介绍JAVA 中的 Collection FrameWork(包括如何写自己的数据结构)?
答:Collection FrameWork 如下:
Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
Map
├Hashtable
├HashMap
└WeakHashMap
Collection 是最基本的集合接口,一个 Collection 代表一组 Object,即 Collection 的元素
(Elements)
Map 提供 key 到 value 的映射
Java面试题(15)请说出你所知道的线程同步的方法
HashTable 中的 put,get,remove
Vector 的相关方法。
原文链接:http://www.bjpowernode.com/javacjwt/388.html
动力节点Java学院整理发布
转载请注明出处
网友评论