1.字符流是什麼java
2.FileReadermysql
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Demo1_FileReader { public static void main(String[] args) throws IOException { // demo1(); FileReader fr = new FileReader("xxx.txt"); int c; while((c = fr.read()) != -1) { //經過項目默認的碼錶一次讀取一個字符,賦值給c System.out.print((char)c); //將讀到的字符強轉後打印 } fr.close(); } private static void demo1() throws FileNotFoundException, IOException { FileReader fr = new FileReader("xxx.txt"); int x = fr.read(); System.out.println(x); char c = (char)x; System.out.println(c); fr.close(); } }
import java.io.FileWriter; import java.io.IOException; public class Demo2_fileWriter { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("yyy.txt"); fw.write("你們好,SE快學完了!"); fw.write(97); fw.close(); } }
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo3_Copy { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("xxx.txt"); FileWriter fw = new FileWriter("zzz.txt"); int c; while((c = fr.read()) != -1) { fw.write(c); } fr.close(); //Writer類中有一個2k的小緩衝區,若是不關流,就會將內容寫到緩衝區裏,關流會將緩衝區內容寫出並結束 fw.close(); } }
public static void main(String[] args) throws IOException { FileReader fr = new FileReader("xxx.txt"); FileWriter fw = new FileWriter("yyy.txt"); char[] arr = new char[1024]; int len; while((len = fr.read(arr)) != -1) { //將文件上的數據讀取到字符數組中 fw.write(arr, 0, len); //將字符數組中的數據寫到文件上 } fr.close(); fw.close(); }
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("xxx.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt")); int c; while((c = br.read()) != -1) { //read一次,會先將緩衝區讀滿,從緩衝去中一個一個的返給臨時變量c bw.write(c); //write一次,是將數據裝到字符數組,裝滿後再一塊兒寫出去 } br.close(); bw.close(); }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo4_Buffered { public static void main(String[] args) throws IOException { // demo1(); BufferedReader br = new BufferedReader(new FileReader("zzz.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("aaa.txt")); String line; while((line = br.readLine()) != null) { bw.write(line); bw.newLine(); //寫出回車換行符,跨平臺的 bw.write("\r\n"); //只支持windows系統 } br.close(); bw.close(); } private static void demo1() throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader("zzz.txt")); String line; while((line = br.readLine()) != null) { System.out.println(line); } br.close(); } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; public class Demo5_test { public static void main(String[] args) throws IOException { //1.建立輸入輸出流對象 BufferedReader br = new BufferedReader(new FileReader("zzz.txt")); //2.建立集合對象 ArrayList<String> list = new ArrayList<>(); //3.將讀到的數據存儲在集合中 String line; while((line = br.readLine()) != null) { list.add(line); } br.close();//晚開早關 //4.倒着遍歷集合將數據寫到文件上 BufferedWriter bw = new BufferedWriter(new FileWriter("revzzz.txt"));//晚開早關 for (int i = list.size() - 1; i >= 0; i--) { bw.write(list.get(i)); bw.newLine(); } //5.關流 bw.close(); } }
LineNumberReader是BufferedReader的子類, 具備相同的功能, 而且能夠統計行號spring
import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; public class Demo6_LineNumberReader { public static void main(String[] args) throws IOException { LineNumberReader lnr = new LineNumberReader(new FileReader("zzz.txt")); String line; lnr.setLineNumber(100); //設置行號 while((line = lnr.readLine()) != null) { System.out.println(lnr.getLineNumber() + ":" + line); //獲取行號 } lnr.close(); } }
public class Demo7_Wrap { public static void main(String[] args) { AllIdea ai = new AllIdea(new Student()); ai.code(); } } interface Coder{ public void code(); } class Student implements Coder{ @Override public void code() { System.out.println("JavaSE"); System.out.println("JavaWEB"); } } class AllIdea implements Coder{ //1.獲取被裝飾類的引用 private Student s; //2.在構造方法中傳入被裝飾的對象 public AllIdea(Student s) { this.s = s; } //3.對原有的功能進行升級 @Override public void code() { s.code(); System.out.println("mysql"); System.out.println("spring"); } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class Demo8_TransIO { public static void main(String[] args) throws IOException { // demo1(); // demo2(); BufferedReader br = //更高效的讀 new BufferedReader(new InputStreamReader(new FileInputStream("utf-8.txt"), "utf-8")); BufferedWriter bw = //更高效的寫 new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gbk.txt"), "gbk")); int c; while((c = br.read()) != -1) { bw.write(c); } br.close(); bw.close(); } private static void demo2() throws UnsupportedEncodingException, FileNotFoundException, IOException { //用指定碼錶讀字符 InputStreamReader isr = new InputStreamReader(new FileInputStream("utf-8.txt"), "utf-8"); //用指定碼錶寫字符 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"), "gbk"); int c; while((c = isr.read()) != -1) { osw.write(c); } isr.close(); osw.close(); } private static void demo1() throws FileNotFoundException, IOException { //用默認碼錶讀寫,出現亂碼 FileReader fr = new FileReader("utf-8.txt"); FileWriter fw = new FileWriter("gbk.txt"); int c; while((c = fr.read()) != -1) { fw.write(c); } fr.close(); fw.close(); } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.TreeMap; public class Demo91_test { public static void main(String[] args) throws IOException { //1.建立帶緩衝的輸入流對象 BufferedReader br = new BufferedReader(new FileReader("zzz.txt")); //2.建立雙列集合對象TreeMap TreeMap<Character, Integer> tm = new TreeMap<>(); //3.將讀到的字符存儲在雙列集合中,存儲的時候要作判斷,若是不包含這個鍵,就將鍵和1存儲, //若是包含這個鍵,就將該鍵和值加1存儲 int ch; while((ch = br.read()) != -1) { char c = (char)ch; //向下強制類型轉換 /*if(!tm.containsKey(c)) { tm.put(c, 1); }else { tm.put(c, tm.get(c) + 1); }*/ tm.put(c, !tm.containsKey(c) ? 1 : tm.get(c) + 1); } //4.關閉輸入流 br.close(); //5.建立輸出流對象 BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt")); //6.遍歷集合將集合中的內容寫到times.txt中 for (Character key : tm.keySet()) { switch (key) { case '\t': bw.write("\\t" + "=" + tm.get(key)); break; case '\n': bw.write("\\n" + "=" + tm.get(key)); break; case '\r': bw.write("\\r" + "=" + tm.get(key)); break; default: bw.write(key + "=" + tm.get(key)); //寫出鍵和值 break; } bw.newLine(); } //7.關閉輸出流 bw.close(); } }
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo92_test { public static void main(String[] args) throws IOException { //1.建立帶緩衝的輸入流對象,由於要使用readLine方法,能夠保證數據的原樣性 BufferedReader br = new BufferedReader(new FileReader("config.txt")); //2.將讀到的字符串轉換爲int數 String line = br.readLine(); int times = Integer.parseInt(line); //將數字字符串轉換爲數字 //3.對int數進行判斷,若是大於0,就將其一一寫回去,若是不大於0,就提示請購買正版。 if(times > 0) { //4.在if判斷中要將--的結果打印,並將結果經過輸出流寫到文件上 System.out.println("您還有" + times-- + "次機會"); FileWriter fw = new FileWriter("config.txt"); fw.write(times-- + ""); fw.close(); }else { System.out.println("您的試用次數已到,請購買正版"); } br.close(); } }
概念:方法本身調用本身sql
5 * 4 * 3 * 2 * 1 5 * fun(4)(表明4!) 4 * fun(3)(表明3!) 3 * fun(2)(表明2!) 2 * fun(1)(表明1!)
public class Demo93_recursive { public static void main(String[] args) { // demo1(); System.out.println(fun(10)); } public static int fun(int num) { if(num == 1) { return 1; }else { return num * fun(num - 1); } } private static void demo1() { int result = 1; for(int i = 1; i <= 5; i++) { result = result * i; } System.out.println(result); } }
import java.io.File; import java.util.Scanner; public class Demo94_test { public static void main(String[] args) { File dir = getDir(); printJavaFile(dir); } /*獲取鍵盤錄入的文件夾路徑 * 1.返回值類型File * 2.不須要有參數*/ public static File getDir() { Scanner sc = new Scanner(System.in); //建立鍵盤錄入對象 System.out.println("請輸入一個文件夾路徑"); while (true) { String line = sc.nextLine(); //將鍵盤錄入的文件夾路徑存儲 File dir = new File(line); if(!dir.exists()) { System.out.println("您錄入的文件夾路徑不存在,請從新錄入"); }else if(dir.isFile()) { System.out.println("您錄入的是文件路徑,請從新錄入文件夾路徑"); }else { return dir; } } } /*獲取文件夾路徑下的全部.java文件 * 1.返回值類型void * 2.參數列表File dir*/ public static void printJavaFile(File dir) { //1.獲取到該文件夾路徑下的全部文件和文件夾,存儲在File數組中 File[] subFiles = dir.listFiles(); //2.遍歷數組,對每個文件或文件夾判斷 for(File subFile : subFiles) { //3.若是是文件,而且是.java的就打印 if(subFile.isFile() && subFile.getName().endsWith(".java")) { System.out.println(subFile); //4.若是是文件夾,就遞歸調用 }else if(subFile.isDirectory()) { printJavaFile(subFile); } } } }