原文:http://blog.csdn.net/aaaaaaaa0705/article/details/6732893
(1)
题目:给出一个整数数组,将其写入一个文件,再从文件中读出,并按整数大小逆序打印。
这道面试题是我看了好多面试题后自己编的,希望能对将要面试的人有点帮助。
对于我这个新手来说,觉得这个题目考察的基础东西还是比较多的:
1.对文件的写入和读出方面的考察
2.对不同形的数组转换方面的考察
3.对string基本用法的考察(split,substring)
4.类型转换的考察
5.对算法的考察(排序)
ps:我写的这个题目肯定有其他更好的方法,我这个就算是一个笨方法了,由于我编程功力还不深厚,而且写下面的这些代码借助了myEclipse这样先进的IDE工具,还时不时的上baidu ,google搜索一下,但在笔试的时候全屏记忆在纸上写,这样难度可想而知。看来基础真的很重要
- package com.xyq.demo;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- /**
- *
- * @author xyq
- *
- * 作用:一道面试题
- */
- public class T1 {
- /**
- * 把数组写入文件中
- * @throws IOException
- */
- void writefile() throws IOException {
- String str = "";
- FileOutputStream fos = new FileOutputStream("Result.txt");
- OutputStreamWriter osw = new OutputStreamWriter(fos);
- BufferedWriter bw = new BufferedWriter(osw);
- int[] intArr = new int[] { 10, -101,2, 5, 3, 4, 6 };
- //注意在这里输入到文件中的是字符串类型String而不是数组类型
- for (int i = 0; i < intArr.length; i++) {
- str += intArr[i] + ",";
- }
- str = str.substring(0, str.length()-1);
- bw.write(str);
- bw.close();
- }
- /**
- * 把数组从文件中读出来,并排序
- * @throws IOException
- */
- void readfile() throws IOException {
- String str = "";
- String str1 = "";
- FileInputStream fis = new FileInputStream("Result.txt");
- InputStreamReader isr = new InputStreamReader(fis);
- BufferedReader br = new BufferedReader(isr);
- //从文件中读出字符串String类型
- str = br.readLine();
- //把字符串转换成数组类型,再进行排序
- String[] strArr = str.split(",");
- //把String数组转换成int数组
- int[] intArr=new int[strArr.length];
- for(int i=0;i<strArr.length;i++){
- intArr[i]=Integer.parseInt(strArr[i]);
- }
- sort(intArr);
- for (int i = 0; i < intArr.length; i++) {
- str1 += intArr[i]+",";
- }
- str1 = str1.substring(0, str1.length()-1);
- System.out.print(str1);
- br.close();
- }
- /**
- * 冒泡排序算法
- * @param data
- */
- public static void sort(int[] data) {
- int temp;
- for (int i = 0; i < data.length; i++) {
- for (int j = data.length - 1; j > i; j--) {
- if (data[i] < data[j]) {
- temp = data[i];
- data[i] = data[j];
- data[j] = temp;
- }
- }
- }
- }
- /**
- * main方法
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- new T1().writefile();
- new T1().readfile();
- }
- }
运行结果:
在Result.txt中为:10,-101,2,5,3,4,6
控制台打印为:10,6,5,4,3,2,-101
Input和Output是同一类,而Reader和Writer另属同一类
Reader支持16位的Unicode字符的输出,而InputStream只支持8位字符输出。他们的大概结构如下:
InputStream的子类有:
FileInputStream,FilterInputStream,ObjectInputStream,StringBufferInputStream等
OutputStream的子类有:
ByteArrayOutputStream,FileOutputStream,FilterOutputStream,PipedOutputStream,ObjectOutputStream。
Reader的子类有:BufferdReader,InputStreamReader,FilterReader,StringReader,PipedReader,CharArrayReader。
Writer的子类有: BufferedWriter,CharArrayWriter,FilterWriter,OutputStreamWriter,PipedWriter,PrintWriter,StringWriter。
曾遇到一个面试题:
请选择下面的这却答案:
a. System.out 是一个PrintStream。
b. System.out 是一个OutputStream。
c. System.out 是一个FilterOutputStream。
d. System.out 不是一个PrintStream。
e. System.out 在异常时,将抛出IOExcepton。
由于System.out 是一个PrintStream的一个子类,并且PrintStream对象并没有抛出IOException异常。
所以可以看出答案:a b c
例一:InputStream读取文件的应用:
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- <span style="color: #339966;">/**
- *
- * @author liugao ec06 cumt
- *
- */</span>
- public class TestJavaIo {
- <span style="color: #339966;">/**
- * @param args
- */</span>
- public static void main(String[] args) {
- int b=0;
- long num=0;
- InputStream in=null;
- try {
- in=new FileInputStream("D:/a.txt");
- } catch (FileNotFoundException e) {
- System.out.println("文件找不到");
- System.exit(-1);
- }
- try{
- while((b=in.read()) !=-1){ <span style="color: #339966;">//b读取是字符的AI码</span>
- System.out.println((char)b);
- num++;
- }
- in.close();
- System.out.println();
- System.out.println("共读取了" + num + "个字节");
- }catch(IOException e){
- System.out.println("文件读取错误");
- System.exit(-1);
- }
- }
- }
例二:FileReader的应用:
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- <span style="color: #339966;">/**
- *
- * @author ec06cumt
- *
- */</span>
- public class TestFileReader {
- <span style="color: #339966;"> /**
- * @param args
- */</span>
- public static void main(String[] args) {
- FileReader fr=null;
- int c=0;
- int ln=0;
- try {
- fr=new FileReader("D:/a.txt");
- while((c=fr.read())!=-1){
- System.out.println((char)c);
- ln++;
- }
- fr.close();
- <span style="color: #339966;">//主要这里的ln的值,当有中文字符时,一个中文字符还是算一个,
- //但InputStream时就一个中文就两个,由此可以看出Reader和Input的区别</span>
- System.out.println("共有:"+ln+"个字符");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
例三:BufferedReader与BufferWriter的应用:
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- <span style="color: #339966;">/**
- * BufferReader和BufferWriter的应用示例
- * @author ec06cumt
- *
- */</span>
- public class TestBufferedReader {
- <span style="color: #339966;">/**
- * @param args
- */</span>
- public static void main(String[] args) {
- try {
- <span style="color: #339966;">//BuffererReader等就像在原来的字节流的基础上套一个更大的水管,
- //让出水量更大读取的速度更快。</span>
- BufferedWriter bw=new BufferedWriter(new FileWriter("D:/aa.txt"));
- BufferedReader br=new BufferedReader(new FileReader("D://aa.txt"));
- String s=null;
- for(int i=0;i<=100;i++){
- s=String.valueOf(10+(long)(Math.random()*30));
- bw.write(s);
- bw.newLine(); <span style="color: #339966;">//创建一个换行的标记</span>
- }
- bw.flush(); <span style="color: #339966;"> //刷新缓冲区域。</span>
- while((s=br.readLine())!=null){ <span style="color: #339966;">//readLine 就是水管的一个应用吧</span>
- System.out.println(s);
- }
- bw.close();
- br.close();
- } catch (IOException e) {
- System.out.println("写入错误");
- }
- }
- }
例四:DataStream的应用:
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- <span style="color: #339966;">/**
- *
- * @author ec06cumt
- *
- */</span>
- public class TestDataStream {
- <span style="color: #339966;">/**
- * @param args
- */</span>
- public static void main(String[] args) {
- ByteArrayOutputStream bos=new ByteArrayOutputStream();
- DataOutputStream dos=new DataOutputStream(bos);
- try {
- <span style="color: #339966;">// dos.writeFloat((float) Math.random());
- // dos.writeLong((long) Math.random());
- // dos.writeDouble((double) Math.random());
- // dos.writeChar((char) Math.random());</span>
- dos.writeShort((short) Math.random());
- dos.writeBoolean(true);
- //注意ByteArrayInputStream的构造方法是参数要是一个数组
- ByteArrayInputStream bais=new ByteArrayInputStream(bos.toByteArray());
- DataInputStream dis=new DataInputStream(bais);
- System.out.println(dis.available());
- //System.out.println(dis.readDouble());
- <span style="color: #339966;">// System.out.println(dis.readInt());
- // System.out.println(dis.readFloat());
- // System.out.println(dis.readDouble());
- // System.out.println(dis.readChar());</span>
- System.out.println(dis.readShort());
- System.out.println(dis.readBoolean());
- bos.close();
- bais.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
例五:ObjectStream的应用
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- <span style="color: #339966;">/**
- * 对象的写入和读取
- * @author ec06cumt
- *
- */</span>
- public class TestObjectStream {
- <span style="color: #339966;">/**
- * @param args
- */</span>
- public static void main(String[] args) {
- T t=new T();
- t.k=10;
- try {
- FileOutputStream fos=new FileOutputStream("D:/testObjectIo.bak");
- ObjectOutputStream oos=new ObjectOutputStream(fos);
- oos.writeObject(t);
- oos.flush();
- oos.close();
- FileInputStream fis=new FileInputStream("D:/testObjectIo.bak");
- ObjectInputStream bis=new ObjectInputStream(fis);
- T tReader= (T) bis.readObject();
- System.out.println(tReader.i + " " +tReader.j + " "+tReader.d + " " + tReader.k);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch(IOException e1){
- e1.printStackTrace();
- }catch(ClassNotFoundException e2){
- e2.printStackTrace();
- }
- }
- }
- class T implements Serializable{
- int i=2;
- int j=4;
- double d=2.5;
- transient int k=15;
- // int k=15;
- }
例六:文件的目录复制
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class JavaIoCopyFile {
- <span style="color: #339966;">/**
- * Author:liugao ec06-2 cumt
- * @param args
- */</span>
- public static void main(String[] args) {
- File sourceFile=new File("D:/test");
- File targetFile=new File("F:/jstl");
- copy(sourceFile,targetFile);
- }
- private static void copy(File sourceFile, File targetFile) {
- File tarpath=new File(targetFile,sourceFile.getName());
- if(sourceFile.isDirectory()){
- tarpath.mkdir();
- File[] dir=sourceFile.listFiles();
- for(int i=0;i<dir.length;i++){
- copy(dir[i],tarpath);
- }
- }else{
- try {
- InputStream is=new FileInputStream(sourceFile);
- OutputStream os=new FileOutputStream(tarpath);
- byte[] buf=new byte[1024];
- int len=0;
- while((len= is.read(buf))!=-1){
- os.write(buf);
- }
- System.out.println("复制完成");
- is.close();
- os.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch(IOException e1){
- e1.printStackTrace();
- }
- }
- }
- }
常用的也就是这些方法吧。。。。