今天是周四,上午阴天,有点小雨飘过,气温还挺低,有种恍惚的上学的感觉。昨天晚上的事情真的是烦死我了,这也许就是没规划好的缘故吧,导致什么都赶不及。今天下午和张子轩聊了一会儿,他这都拿到了诺丁汉的offer了,效率可真高,刚才发现,原来诺丁汉大学对双一流高校的均分要求是75分啊,心态爆炸了(捂脸),天津工业大学双一流倒是没白评,真好啊。张子轩原来是当场给的offer,这么厉害呢,他倒是很谦虚,说自己是碰上了,不过即使是运气成分也很好了。晚上和佟姐孟姐聊天的时候,她们还在劝我既然在公司里都这么自闭了,还不如回去好好准备英语,孟姐说,在哪写代码不是写。要不,拿了offer就辞职吧。大概再等两个月就辞职?我也不知道怎么办了。先把杭州人才补贴拿到手再说吧。就这样,洗洗澡睡觉了,佟姐说得对,要有自己的想法,不能总听别人说。是啊,这么没有主见,不行啊。
package homework; import java.io.*; /** * 用FileInputStream 字节流正确读取中文 * 为了能够正确的读取中文内容 * 1. 必须了解文本是以哪种编码方式保存字符的 * 2. 使用字节流读取了文本后,再使用对应的编码方式去识别这些数字,得到正确的字符 * 如本例,一个文件中的内容是字符中,编码方式是GBK,那么读出来的数据一定是D6D0。 * 再使用GBK编码方式识别D6D0,就能正确的得到字符中 */ public class Test { public static void main(String[] args) { File f = new File("D:\\xyz\\test3.txt"); try (FileInputStream fis = new FileInputStream(f)){ //创建字节数组,其长度就是文件的长度 byte[] all = new byte[(int)f.length()]; fis.read(all); //文件中读出来的数据是 System.out.println("文件中读出来的数据是:"); for (byte b : all) { int i = b&0x000000ff; //只取16进制的后两位 System.out.print(Integer.toHexString(i)+" "); } System.out.println(); System.out.println("把这个数字,放在GBK的棋盘上去:"); String str = new String(all,"GBK"); System.out.println(str); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package homework; import java.io.*; import java.nio.charset.Charset; /** * 用FileReader 字符流正确读取中文 */ public class Test { public static void main(String[] args) { File f = new File("D:\\xyz\\test3.txt"); System.out.println("默认编码方式:"+ Charset.defaultCharset()); //FileReader得到的是字符,所以一定是已经把字节根据某种编码识别成了字符了,依据默认编码UTF-8 try(FileReader fr = new FileReader(f)) { char[] cs = new char[(int) f.length()]; fr.read(cs); System.out.printf("FileReader会使用默认的编码方式%s,识别出来的字符是:%n",Charset.defaultCharset()); System.out.println(new String(cs)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //FileReader是不能手动设置编码方式的,为了使用其他的编码方式,只能使用InputStreamReader来代替 //并且使用new InputStreamReader(new FileInputStream(f),Charset.forName("UTF-8")); 这样的形式 try (InputStreamReader isr = new InputStreamReader(new FileInputStream(f),Charset.forName("UTF-8"))){ char[] cs = new char[(int)f.length()]; isr.read(cs); System.out.printf("InputStreamReader 指定编码方式UTF-8,识别出来的字符是:%n"); System.out.println(new String(cs)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package homework; import java.io.*; /** * 找出 E5 B1 8C 这3个十六进制对应UTF-8编码的汉字 */ public class Test { public static void main(String[] args) throws UnsupportedEncodingException { //找出 E5 B1 8C 这3个十六进制对应UTF-8编码的汉字 byte[] bs = new byte[3]; bs[0] = (byte) 0xE5; bs[1] = (byte) 0xB1; bs[2] = (byte) 0x8C; String str =new String(bs,"UTF-8"); System.out.println("E5 B1 8C 对应的字符是:"+str); } }
package homework; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Arrays; /** * 如果用记事本根据UTF-8编码保存汉字就会在最前面生成一段标示符,这个标示符用于表示该文件是使用UTF-8编码的。 * * 找出这段标示符对应的十六进制,并且开发一个方法,自动去除这段标示符 */ public class Test { public static void main(String[] args) { File file = new File("D:\\xyz\\test3.txt"); try (FileInputStream fis = new FileInputStream(file)){ byte[] all = new byte[(int) file.length()]; fis.read(all); System.out.println("首先确认按照UTF-8识别出来有?"); String str = new String(all, StandardCharsets.UTF_8); System.out.println(str); System.out.println("根据前面的所学,知道'***'字对应的UTF-8编码是:e5 b1 8c"); System.out.println("打印出文件里所有的数据的16进制是:"); for (byte b : all) { //仅取最后两位,原:ffffffef ffffffbb 后ef bb int i = b & 0xff; System.out.print(Integer.toHexString(i)+" "); } System.out.println(); System.out.println("通过观察法得出UTF-8的BOM是 ef bb bf"); System.out.println("去掉了BOM之后的数据的16进制是:"); byte[] removed = new byte[all.length]; removed = removeBom(all); for (byte b : removed) { int i = b & 0xff; System.out.print(Integer.toHexString(i)+" "); } System.out.println(); System.out.println("对应的字符串就没有问号了:"); String strWithoutBom = new String(removed,StandardCharsets.UTF_8); System.out.println(strWithoutBom); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static byte[] removeBom(byte[] all){ byte[] removed = Arrays.copyOfRange(all,3,all.length); return removed; } }
package homework; import java.io.*; /** * 设计一个方法,用于移除Java文件中的注释 * * public void removeComments(File javaFile) * * * 比如,移出以//开头的注释行 * * File f = new File("d:/LOLFolder/LOL.exe"); * System.out.println("当前文件是:" +f); * //文件是否存在 * System.out.println("判断是否存在:"+f.exists()); * //是否是文件夹 * System.out.println("判断是否是文件夹:"+f.isDirectory()); */ public class Test { public static void main(String[] args) { File javaFile = new File("E:\\project\\j2se\\src\\HelloJava.java"); removeComments(javaFile); } public static void removeComments(File javaFile){ StringBuffer sb = new StringBuffer(); //读取数据 try (FileReader fr = new FileReader(javaFile); BufferedReader br = new BufferedReader(fr)){ while (true){ String line = br.readLine(); if (line == null){ break; } if (!line.trim().startsWith("//")){ //在sb追加一行的同时回车,保持格式 sb.append(line+System.getProperty("line.separator")); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //写出数据 try (FileWriter fw = new FileWriter(javaFile); PrintWriter pw = new PrintWriter(fw)){ pw.write(sb.toString()); } catch (IOException e) { e.printStackTrace(); } } }