[关闭]
@Sarah 2015-06-22T14:14:34.000000Z 字数 6675 阅读 1041

JAVA LESSON 8

  1. //上堂课我的作业更正版本
  2. package file;
  3. import java.io.*;
  4. public class file {
  5. public static void main(String[] args) throws Exception {
  6. {
  7. try {
  8. FileWriter fw = new FileWriter("File.txt");
  9. InputStreamReader x = new InputStreamReader(System.in);
  10. BufferedReader y = new BufferedReader(x);
  11. int i=1;
  12. for (i++) {
  13. System.out.print(i+":");
  14. String s = y.readLine();
  15. if (s.equals("[end]")) {
  16. fw.close();
  17. break;
  18. } else {
  19. fw.write(s+"\n");
  20. }
  21. }
  22. } catch (Exception e) {
  23. }
  24. }
  25. }
  26. }

File Filter 控制文件的信息

可以同时写入和读取文件 rw
可以显示文件最后一次修改的时间
可以把这个时间格式化成年月日格式
可以用f.mkdirs新建一组文件夹“\a\b\c\d”
可以从第几个开始读取 遍历之后的内容
FileNameFilter()

  1. //读取abc.txt里第几个字符
  2. import java.io.*;
  3. public class RandomAccess {
  4. public static void main(String[] args) {
  5. try {
  6. RandomAccessFile af=new RandomAccessFile("abc.txt", "rw");
  7. //rw可以读取和写入
  8. for(int i=0;i<256;i++)
  9. af.write(i);
  10. af.seek(0);//从第0个开始
  11. int s=af.readInt();
  12. System.out.println(s);//第0个是什么?
  13. af.skipBytes(65);//第65个? E
  14. System.out.println((char)af.readByte());
  15. af.skipBytes(-5);//输出的是第67个F
  16. System.out.println((char)af.readByte());
  17. af.close();
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }

✨额外的大案例

  1. import java.io.*;
  2. public class DisplayFile {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. if(args.length<1){
  6. System.out.println("Usage:JAVA DisplayFile <filename> [-l length] [-t]");
  7. System.out.println("\t-l:length of file contents to display.");
  8. System.out.println("\t-t:display content with text mode.");
  9. System.exit(-1);
  10. }
  11. String filename=args[0];
  12. boolean hex=true;
  13. int length=256;
  14. for(int i=1;i<args.length;i++){
  15. if(args[i].toLowerCase().equals("-l")){
  16. if(args.length<i+2){
  17. System.out.println("please tell me length of file content.");
  18. System.exit(-2);
  19. }
  20. else
  21. try{
  22. length=Integer.parseInt(args[i+1]);
  23. }
  24. catch(NumberFormatException e){
  25. System.out.println(e);
  26. System.exit(-3);
  27. }
  28. i++;
  29. continue;
  30. }
  31. if(args[i].toLowerCase().equals("-t"))
  32. hex=false;
  33. }
  34. byte[] content=new byte[length];
  35. try {
  36. FileInputStream fis=new FileInputStream(filename);
  37. length=fis.read(content);
  38. if(hex)
  39. displayHex(content,length);
  40. else
  41. displayText(content,length);
  42. } catch (FileNotFoundException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. } catch (IOException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. }
  50. private static void displayText(byte[] c,int length){
  51. System.out.print(new String(c,0,length));
  52. }
  53. private static void displayHex(byte[] c,int length){
  54. int i=0;
  55. System.out.print(" Offset");
  56. for(int j=0;j<5;j++)
  57. System.out.print(" ");
  58. System.out.print("Content(hex. format)");
  59. for(int j=0;j<6;j++)
  60. System.out.print(" ");
  61. System.out.print("Content(text format)\n");
  62. for(int j=0;j<80;j++)
  63. System.out.print("-");
  64. System.out.println();
  65. while(i<length){
  66. int j=0,k=0;
  67. System.out.printf("%08X ", i);
  68. while(j<16){
  69. System.out.printf("%02X ", c[i+j]);
  70. j++;
  71. if(i+j==length)
  72. break;
  73. }
  74. if(j<16)
  75. for(k=0;k<16-j;k++)
  76. System.out.print(" ");
  77. k=0;
  78. System.out.print(" ");
  79. while(k<j){
  80. System.out.print((char)(c[i+k]>=32 && c[i+k]<=126?c[i+k]:'.'));
  81. k++;
  82. }
  83. System.out.println();
  84. i+=16;
  85. }
  86. }
  87. }
  1. import java.io.*;
  2. import java.util.Date;
  3. import java.text.SimpleDateFormat;
  4. class FileFilterTest{
  5. public static void main(String args[]){
  6. File dir=new File("./");
  7. Filter filter=new Filter(".class");
  8. String files[]=dir.list();
  9. for(int i=0;i<files.length;i++){
  10. File f=new File(files[i]);
  11. Date d=new Date(f.lastModified());
  12. String df="yyyy-MM-dd HH:mm:ss";
  13. String sdf=new SimpleDateFormat(df).format(d);
  14. System.out.print(sdf+"\t");
  15. if (f.isFile())
  16. System.out.print("\t"+f.length()+"\t");
  17. else
  18. System.out.print("<DIR>\t\t");
  19. System.out.print(f.getName()+"\n");
  20. }
  21. }
  22. }
  23. class Filter implements FilenameFilter{
  24. String extent;
  25. Filter(String extent){
  26. this.extent=extent;
  27. }
  28. public boolean accept(File dir,String name){
  29. return name.endsWith(extent);
  30. }
  31. }

难※ File.list(.)

(.)表示当前文件夹
mf.accept(File,String)

可以在制定文件夹里寻找后缀/文件夹名字是指定字符的文件,比如找.txt文件,是true的话就留着,是false就从遍历里去掉,然后返回符合的几个文件名字,

list(FilenameFilter m)
m.accept(s[i])
m.accept

  1. //输出文件最后修改的日期并shimiaofeng格式化为年月日十秒分格式
  2. import java.io.*;
  3. import java.text.SimpleDateFormat;
  4. public class FIleClassTest {
  5. public static void main(String[] args) {
  6. try{
  7. File f=new File("./");
  8. if(f.exists()){
  9. if(f.isFile()){
  10. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  11. String s=sdf.format(f.lastModified());
  12. System.out.println(s);
  13. // FileReader fr=new FileReader(f);
  14. }else{
  15. MyFilter mf=new MyFilter();
  16. String files[]=f.list(mf);
  17. for(String s:files)
  18. System.out.println(s);
  19. }
  20. }else{
  21. }
  22. }
  23. catch(Exception e){
  24. System.out.println(e);
  25. }
  26. }
  27. }
  1. //为了实现FilenameFilter建立的接口
  2. import java.io.File;
  3. import java.io.FilenameFilter;
  4. public class MyFilter implements FilenameFilter {
  5. @Override
  6. public boolean accept(File arg0, String arg1) {
  7. // TODO Auto-generated method stub
  8. if(arg1.indexOf("a")>=0)
  9. return true;
  10. else
  11. return false;
  12. }
  13. }

增强版for循环

  1. //新写法
  2. for (int a : arr)
  3. System.out.println(a);
  4. //旧写法
  5. for (int i = 0; i < arr.length; i++)
  6. System.out.println(arr[i]);

序列化

就是输出一排特别整齐的东西在terminal里那个!能显示出文件里的内容

try catch可以自动写 选中要try catch的代码 右键

序列化:我们可以通过序列化来保存一个对象的状态(实例变量)到文件中,也可以从这个格式化的文件中很容易地读取对象的状态从而可以恢复我们保存的对象。
用来实现序列化的类都在java.io包中,我们常用的类或接口有:
ObjectOutputStream:提供序列化对象并把其写入流的方法
ObjectInputStream:读取流并反序列化对象
Serializable:一个对象想要被序列化,那么它的类就要实现 此接口

  1. package Circle;
  2. import java.io.Serializable;
  3. public class Point implements Serializable{
  4. int x,y;
  5. private Point(){
  6. this(0,0);
  7. }
  8. public Point(int bb){
  9. }
  10. public Point(int x,int y){
  11. this.x=x;
  12. this.y=y;
  13. }
  14. Point getPoint(){
  15. Point p=new Point();
  16. p.setX(-x);
  17. p.setY(-y);
  18. return p;
  19. }
  20. public void setX(long xx){
  21. x=(int)xx;
  22. }
  23. void setX(int xx){
  24. x=xx;
  25. }
  26. void setY(int yy){
  27. y=yy;
  28. }
  29. Point getApoint(){ //新函数 返回值是point
  30. Point q=new Point(-x,-y);
  31. return q;
  32. }
  33. public double distance(Point p2){ //point是原来的点坐标,p2是另一点的坐标
  34. double d=0;
  35. d=Math.sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));
  36. return d;
  37. }
  38. public boolean equals(Point p3){
  39. if (p3.x==x && p3.y==y) {
  40. return true;
  41. }
  42. else {
  43. return false;
  44. }
  45. }
  46. public String toString(){
  47. return "["+x+","+y+"]";
  48. }
  49. public int getX(){
  50. return x;
  51. }
  52. public int getY() {
  53. return y;
  54. }
  55. }
  1. //创建一个接口
  2. package Circle;
  3. import java.io.*;
  4. public class SerializableTest {
  5. public static void main(String[] args) {
  6. try {
  7. ObjectInputStream oi=new ObjectInputStream(new FileInputStream("point"));
  8. Point p1=(Point)oi.readObject();
  9. System.out.println(p1);
  10. } catch (FileNotFoundException e) {
  11. e.printStackTrace();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. } catch (ClassNotFoundException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }

线程

简单地程序都是一个线程
进程process
多线程的意思就是,程序可以边打字边数日期,两件事互相不干扰,各自进行。

  1. //在两个变量数数的同时读取键盘输入 主函数
  2. import java.io.*;
  3. public class Main {
  4. public static void main(String[] args)throws Exception {
  5. BufferedReader br=new BufferedReader(
  6. new InputStreamReader(System.in)
  7. );
  8. MyThread t=new MyThread();
  9. Thread r=new Thread(t);
  10. r.start();
  11. Thread.sleep(1500);
  12. MyThread t1=new MyThread();
  13. new Thread(t1).start();
  14. while(true){
  15. String s=br.readLine();
  16. System.out.println(s);
  17. }
  18. }
  19. }
  1. //在两个变量数数的同时读取键盘输入 接口的函数
  2. //public class MyThread extends Thread {
  3. public class MyThread extends Object implements Runnable {
  4. //这里是个接口 因为有runnable所以不能写Thread的函数,不然有两个接口了
  5. public void run(){
  6. int i=0;
  7. while(true){
  8. System.out.println("\t"+i);
  9. i++;
  10. if(i==3)
  11. break;//i数数数到3就结束
  12. try {
  13. Thread.sleep(3000);
  14. } catch (InterruptedException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20. }

上面谁要用到接口来着 就是什么T没有函数不能直接用 但是必须叫这个名字 用一个函数调用 所以要自己写接口
可以自动构造函数 然后区别的地方调用一下就可以用了
貌似是list

作业
读取六级文档 并输入到一个数组string里面 可以设置范围是一万


总结

输入流/输出流
字节流/字符流
节点流/处理流

以后用的时候:依稀记得有这么一个buffer东西,然后打开API回去查一下。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注