/********************************idea快捷键************************************/ 输出:sout+Enter /**********************************读取文件的方法: 1、按字节读取文件内容:(模板) public class ReadFromFile{ /** * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 */ public static void readFileByBytes(String fileName) { /*1*/ File file = new File(fileName); InputStream in = null;//inputStream为所有输入类的超类,是抽象类,不能实例化 try { System.out.println("以字节为单位读取文件内容,一次读一个字节:"); // 一次读一个字节 /*2*/ in = new FileInputStream(file);// 文件字节输入流,对文件数据以字节的形式进行读取操作 int tempbyte; /*3*/ while ((tempbyte = in.read()) != -1) {//从输入流中读取一个字节返回int型变量,若到达文件末尾则返回-1 System.out.write(tempbyte); } /*4*/ in.close();//关闭文件输入流 } catch (IOException e) { e.printStackTrace(); return; } try { System.out.println("以字节为单位读取文件内容,一次读多个字节:"); // 一次读多个字节 byte[] tempbytes = new byte[100];//字节数组 int byteread = 0; in = new FileInputStream(fileName); // 读入多个字节到字节数组中,byteread为一次读入的字节数 while ((byteread = in.read(tempbytes)) != -1) {//读入多个字节,存在字节数组中,返回字节数,若到末尾则返回-1 System.out.write(tempbytes, 0, byteread); } } catch (Exception e1) { e1.printStackTrace(); } finally { if (in != null) { try { in.close();//关闭输入流 } catch (IOException e1) { } } } } public static void main(String[] args) { String file = "D:\\1study\\hello.txt"; readFileByBytes(file); } } 2、按字符读取文件内容: /** * 以字符为单位读取文件,常用于读文本,数字等类型的文件 */ public static void readFileByChars(String fileName) { /*1*/ File file = new File(fileName); Reader reader = null; try { System.out.println("以字符为单位读取文件内容,一次读一个字节:"); // 一次读一个字符 /*2*/ reader = new InputStreamReader(new FileInputStream(file)); int tempchar; /*3*/ while ((tempchar = reader.read()) != -1) { // 对于windows下,\r\n这两个字符在一起时,表示一个换行。 // 但如果这两个字符分开显示时,会换两次行。 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。 if (((char) tempchar) != '\r') { System.out.print((char) tempchar); } } /*4*/ reader.close(); } catch (Exception e) { e.printStackTrace(); } } 3、按行读取文件内容: /** * 以行为单位读取文件,常用于读面向行的格式化文件 */ public static void readFileByLines(String fileName) { /*1*/ File file = new File(fileName); BufferedReader reader = null; try { System.out.println("以行为单位读取文件内容,一次读一整行:"); /*2*/ reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 /*3*/ while ((tempString = reader.readLine()) != null) { // 显示行号 System.out.println("line " + line + ": " + tempString); line++; } /*4*/ reader.close(); } catch (IOException e) { e.printStackTrace(); } } 4、随机读取文件内容 : public static void readFileByRandomAccess(String fileName) { RandomAccessFile randomFile = null; try { System.out.println("随机读取一段文件内容:"); // 打开一个随机访问文件流,按只读方式 /*1*/ randomFile = new RandomAccessFile(fileName, "r"); // 文件长度,字节数 /*2*/ long fileLength = randomFile.length(); // 读文件的起始位置 /*3*/ int beginIndex = (fileLength > 4) ? 4 : 0; // 将读文件的开始位置移到beginIndex位置。 /*4*/ randomFile.seek(beginIndex); byte[] bytes = new byte[10]; int byteread = 0; // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。 // 将一次读取的字节数赋给byteread /*5*/ while ((byteread = randomFile.read(bytes)) != -1) { System.out.write(bytes, 0, byteread); } } catch (IOException e) { e.printStackTrace(); } } /** * 显示输入流中还剩的字节数 */ private static void showAvailableBytes(InputStream in) { try { System.out.println("当前字节输入流中的字节数为:" + in.available()); } catch (IOException e) { e.printStackTrace(); } } /*文件末尾追加内容*/ /*使用randomaccessfile追加*/ public static void appendMethodA(String fileName, String content) { try { // 打开一个随机访问文件流,按读写方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 文件长度,字节数 long fileLength = randomFile.length(); //将写文件指针移到文件尾。 randomFile.seek(fileLength); randomFile.writeBytes(content); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用FileWriter追加 */ public static void appendMethodB(String fileName, String content) { try { //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /********************************数组复制的方法: 1. System.arraycopy(Object src,int srcPos,Object dest,int destPos,int length) //源数组、源数组中的起始位置、目标数组、目标数组中的位置、复制长度 2.Arrays.copyOf(Object src,int len) //源数组、复制长度(新数组长度,如果新数组的长度超过原数组的长度,则保留数组默认值) Arrays的copyOf()方法传回的数组是新的数组对象,改变传回数组中的元素值,不会影响原来的数组。 import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { int[] arr1 = {1, 2, 3, 4, 5}; int[] arr2 = Arrays.copyOf(arr1, 5);//1 2 3 4 5 int[] arr3 = Arrays.copyOf(arr1, 10);//1 2 3 4 5 0 0 0 0 0 } } 3.使用clone方法 java.lang.Object类的clone()方法为protected类型,不可直接调用,需要先对要克隆的类进行下列操作: 首先被克隆的类实现Cloneable接口;然后在该类中覆盖clone()方法,并且在该clone()方法中调用super.clone();这样,super.clone()便可以调用java.lang.Object类的clone()方法。 //被克隆的类要实现Cloneable接口 class Cat implements Cloneable { private String name; private int age; public Cat(String name,int age) { this.name=name; this.age=age; } //重写clone()方法 protected Object clone()throws CloneNotSupportedException{ return super.clone() ; } } public class Clone { public static void main(String[] args) throws CloneNotSupportedException { Cat cat1=new Cat("xiaohua",3); System.out.println(cat1); //调用clone方法 Cat cat2=(Cat)cat1.clone();//复制对象,cat2和cat1指向不同地址 Cat cat3=(Cat)cat1;//复制引用,cat3和cat1指向同一个地址 } } /***********map、list、set、数组之间的转换(map--->list/set、set----->数组、set<----->list<------>数组) //------------------------------------map与list------------------------------------------// //把map所有的键key转换为list List list = new ArrayList(map.keySet());//Object为键的类型 //把map所有的值value转换为list List list = new ArrayList(map.values());//Object为值的类型 //-----------------------------------map与set------------------------------------------// //把map所有的键key转换为set Set set = map.keySet(); //把map所有的值value转换为set Set set = map.values(); //----------------------------------set与数组------------------------------------------// String[] arr = new String[set.size()]; set.toArray(arr);//set中的元素复制到arr中 //---------------------------------------------set与list(相互)-------------------------// List list = new ArrayList(set); Set set = new HashSet(list); //--------------------------------------------list与数组(相互)-------------------------// Object[] arr = list.toArray();//1.列表转换为数组 //2.列表转换为数组 Object[] arr = new Object[list.size()]; list.toArray(arr); //数组转换为列表 List list = Arrays.asList(Object[]);