标题图
父类
package hh;public class People { protected String name; protected int age=16; public String getName() { return name; } public void setName(String name) { this.name = name; } public void getAge() { return age; } public void setAge(int age) { this.age = age; } //父类的print方法 public void print(){ System.out.println(this.name); System.out.println(this.age); } }
子类
package hh;//继承使用extends关键字,前边是子类,后边是父类,类的继承只能有一个父类public class Student extends People { private String no; //子类的成员变量和父类的成员变量同名时父类的成员变量被覆盖 protected int age = 20; public String getNo() { return no; } public void setNo(String no){ this.no = no; } //重载:在同一个类里多个方法名字相同,参数不一样 //重写:在子类和父类之间多个方法名相同,参数相同,并且返回值也相同 //调用父类同名的方法,前加上super public void print(){ super.print(); System.out.println(this.no); } }
测试类
package hh;public class Test { public static void main(String[] args) { People dashu = new People(); dashu.setName("dashu"); dashu.setAge(16); //调用父类的print方法 dashu.print(); Student stu = new Student(); stu.setName("hhhhh"); stu.setAge(16); stu.setNo("1024"); //子类对象的调用 stu.print(); } }
设计思想:用随机文件流把文件正向读出并保存到了字符串中,将字符串倒序,显示到控制台。
package test2;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;public class text { public static void main(String[] args) { String filename = "c:/test/FileDemo.java"; File f1=new File(filename); try{ RandomAccessFile raf1=new RandomAccessFile(filename,"r"); byte[] b = new byte[(int)f1.length()]; StringBuffer sb = new StringBuffer(); for(int i = 0;raf1.read(b)!=-1;i++){ sb.append(new String(b,"utf-8")); } System.out.println(sb.reverse().toString()); raf1.close(); }catch(IOException e){ e.printStackTrace(); } } }
图片
public class text1 { public static void main(String[] args) { FileReader fr; BufferedReader br; File file = new File("c:/test/FileDemo.java"); String str; int n = 0; try { fr = new FileReader(file); br = new BufferedReader(fr); while((str = br.readLine()) != null){ n++; System.out.println(n + "." + str); } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } }
图片
文件读写
目的
1 掌握文件读写的几种方法
2 FileOutputStream和FileInputStream类的使用。
3 基本数据类型之间的转换
实现文件读取后转换为大写后写入到目标文件中,其中src是指源文件,des是目标文件目录。
public class FileDemo { //创建一个文件夹 public static void createFolder(String path){ File folder=new File(path); if(folder.exists()){ System.out.println("文件夹已存在!"); }else{ //不存在时去创建 folder.mkdir(); } } //创建一个文件 public static void createFile(String path,String filename){ File file=new File(path,filename); //文件判断是否已存在 if(file.exists()){ System.out.println("文件已存在!"); System.out.println(file.length()); }else{ try{ file.createNewFile(); }catch(IOException e){ System.out.println("文件创建失败!"); } } } //写文件 public static void write(String path,String filename){ try{ String str="0123456789/nac"; String Upstr = str.toUpperCase();// byte b[]=Upstr.getBytes();// FileOutputStream fos=new FileOutputStream(new File(path,filename)); fos.write(b); fos.close(); }catch(FileNotFoundException e){ System.out.println("文件不存在"); }catch(IOException e){ System.out.println("写文件失败"); } } //读文件 public static void read(String path,String filename){ try{ int length=0; String str=""; byte buffer[]=new byte[10]; FileInputStream fis=new FileInputStream(new File(path,filename)); while((length=fis.read(buffer, 0, buffer.length)) !=-1){ str+=new String (buffer, 0, length); } System.out.println(str);// fis.close(); }catch(FileNotFoundException e){ System.out.println("文件不存在"); }catch(IOException e){ e.printStackTrace(); } }// public static void FileReaderCopy(String src,String des){ try{ FileReader fr=new FileReader(src); FileWriter fw=new FileWriter(des); char c[]=new char[1024]; int len=0; while((len=fr.read(c, 0, c.length)) != -1){ fw.write(c, 0, c.length); } fw.close(); fr.close(); } catch(FileNotFoundException e){ System.out.println("文件不存在"); }catch(IOException e){ System.out.println("读写失败"); } } // public static void BufferedReaderCopy(String src,String des){ try{ BufferedReader br=new BufferedReader(new FileReader(src)); BufferedWriter bw=new BufferedWriter(new FileWriter(des)); String str=""; while((str=br.readLine()) != null){ String Upstr = str.toUpperCase();//加入大写的变换 bw.write(Upstr);// bw.newLine(); } bw.close(); br.close(); } catch(FileNotFoundException e){ System.out.println("文件存在"); }catch(IOException e){ System.out.println("读写失败"); } } //复制 public static void copy(String src,String des){ try{ FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(des); int c; while((c=fis.read()) != -1){ fos.write(c); } fos.close(); fis.close(); }catch(FileNotFoundException e){ System.out.println("文件不存在"); }catch(IOException e){ System.out.println("读写失败"); } } //复制文件 public static void copy1(String src,String des){ try{ FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(des); int c; byte buff[]=new byte[1024]; while((c=fis.read(buff,0,buff.length)) != -1){ fos.write(buff,0,c); } fos.close(); fis.close(); }catch(FileNotFoundException e){ System.out.println("文件不存在"); }catch(IOException e){ System.out.println("读写失败"); } } public static void main(String[] args) { // TODO Auto-generated method stub FileDemo.createFolder("c:/test"); FileDemo.createFile("c:/test", "1.txt"); FileDemo.write("c:/test", "1.txt"); FileDemo.read("c:/test", "1.txt"); FileDemo.read("c:/test", "FileDemo.java"); FileDemo.BufferedReaderCopy("c:/test/FileDemo.java", "c:/test/FileDemo2.java"); FileDemo.copy1("c:/test/1.mp3", "c:/test/2.mp3"); } }
作者:达叔小生
链接:https://www.jianshu.com/p/39f620080f5f