建设网站链接,呼伦贝尔网站建设 设计,网站你懂我意思正能量免费软件,seo网站推广什么意思目录 
68、Java 中如何实现序列化#xff0c;有什么意义#xff1f; 
69、Java 中有几种类型的流#xff1f; 
70、写一个方法#xff0c;输入一个文件名和一个字符串#xff0c;统计这个字符串在这个文件中出现的次数。 
71、如何用 Java 代码列出一个目录下所有的文件有什么意义 
69、Java 中有几种类型的流 
70、写一个方法输入一个文件名和一个字符串统计这个字符串在这个文件中出现的次数。 
71、如何用 Java 代码列出一个目录下所有的文件 68、Java 中如何实现序列化有什么意义 序列化就是一种用来处理对象流的机制所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作也可将流化后的对象传输于网络之间。序列化是为了解决对象流读写操作时可能引发的问题如果不进行序列化可能会存在数据乱序的问题。 要实现序列化需要让一个类实现 Serializable 接口该接口是一个标识性接口标注该类对象是可被序列化的然后使用一个输出流来构造一个对象输出流并通过 writeObject(Object)方法就可以将实现对象写出即保存其状态如果需要反序列化则可以用一个输入流建立对象输入流然后通过 readObject 方法从流中读取对象。序列化除了能够实现对象的持久化之外还能够用于对象的深度克隆可以参考第 29 题。 
互联网Java工程师面试题·Java 总结篇·第三弹-CSDN博客 69、Java 中有几种类型的流 字节流和字符流。字节流继承于 InputStream、OutputStream字符流继承于Reader、Writer。在 java.io 包中还有许多其他的流主要是为了提高性能和使用方便。关于 Java 的 I/O 需要注意的有两点 
一是两种对称性输入和输出的对称性字节和字符的对称性 
二是两种设计模式适配器模式和装潢模式。 
另外 Java 中的流不同于 C#的是它只有一个维度一个方向。 
面试题 - 编程实现文件拷贝。这个题目在笔试的时候经常出现下面的代码给出了两种实现方案 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;public final class MyUtil {private MyUtil() {throw new AssertionError();}public static void fileCopy(String source, String target) throws
IOException {try (InputStream in  new FileInputStream(source)) {try (OutputStream out  new FileOutputStream(target)) {byte[] buffer  new byte[4096];int bytesToRead;while((bytesToRead  in.read(buffer)) ! -1) {out.write(buffer, 0, bytesToRead);}}}
}public static void fileCopyNIO(String source, String target) throws
IOException {try (FileInputStream in  new FileInputStream(source)) {try (FileOutputStream out  new FileOutputStream(target)) {FileChannel inChannel  in.getChannel();FileChannel outChannel  out.getChannel();ByteBuffer buffer  ByteBuffer.allocate(4096);while(inChannel.read(buffer) ! -1) {buffer.flip();outChannel.write(buffer);buffer.clear();}}}}
} 
注意上面用到 Java 7 的 TWR使用 TWR 后可以不用在 finally 中释放外部资源 从而让代码更加优雅。 70、写一个方法输入一个文件名和一个字符串统计这个字符串在这个文件中出现的次数。 
代码如下 
import java.io.BufferedReader;
import java.io.FileReader;public final class MyUtil {
// 工具类中的方法都是静态方式访问的因此将构造器私有不允许创建对象(绝对好习惯)
private MyUtil() {throw new AssertionError();
}
/**
* 统计给定文件中给定字符串的出现次数
*
* param filename 文件名
* param word 字符串
* return 字符串在文件中出现的次数
*/
public static int countWordInFile(String filename, String word) {int counter  0;try (FileReader fr  new FileReader(filename)) {try (BufferedReader br  new BufferedReader(fr)) {String line  null;while ((line  br.readLine()) ! null) {int index  -1;while (line.length()  word.length()  (index 
line.indexOf(word))  0) {counter;line  line.substring(index  word.length());}}}} catch (Exception ex) {ex.printStackTrace();}return counter;
}
}71、如何用 Java 代码列出一个目录下所有的文件 
如果只要求列出当前文件夹下的文件代码如下所示 
import java.io.File;class Test12 {public static void main(String[] args) {File f  new File(/Users/Hao/Downloads);for(File temp : f.listFiles()) {if(temp.isFile()) {System.out.println(temp.getName());}}}
} 
如果需要对文件夹继续展开代码如下所示 
import java.io.File;class Test12 {public static void main(String[] args) {showDirectory(new File(/Users/Hao/Downloads));}public static void showDirectory(File f) {_walkDirectory(f, 0);}private static void _walkDirectory(File f, int level) {if(f.isDirectory()) {for(File temp : f.listFiles()) {_walkDirectory(temp, level  1);}}else {for(int i  0; i  level - 1; i) {System.out.print(\t);}System.out.println(f.getName());}}
} 
在 Java 7 中可以使用 NIO.2 的 API 来做同样的事情代码如下所示 
class ShowFileTest {public static void main(String[] args) throws IOException {Path initPath  Paths.get(/Users/Hao/Downloads);Files.walkFileTree(initPath, new SimpleFileVisitorPath() {Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributesattrs)throws IOException {System.out.println(file.getFileName().toString());return FileVisitResult.CONTINUE;}});}
} 要想了解更多 
千题千解·Java面试宝典_时光の尘的博客-CSD  文章转载自: http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.brcdf.cn.gov.cn.brcdf.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.mjyrg.cn.gov.cn.mjyrg.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.fbxdp.cn.gov.cn.fbxdp.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.yqrgq.cn.gov.cn.yqrgq.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.tqrxm.cn.gov.cn.tqrxm.cn http://www.morning.wkrkb.cn.gov.cn.wkrkb.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.zzgkk.cn.gov.cn.zzgkk.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.dbnpz.cn.gov.cn.dbnpz.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.kmldm.cn.gov.cn.kmldm.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.zgdnz.cn.gov.cn.zgdnz.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.dztp.cn.gov.cn.dztp.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.nptls.cn.gov.cn.nptls.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.xqcbz.cn.gov.cn.xqcbz.cn http://www.morning.kqwsy.cn.gov.cn.kqwsy.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn