日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA自学笔记21

發(fā)布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA自学笔记21 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JAVA自學(xué)筆記21

1、轉(zhuǎn)換流
由于字節(jié)流操作中文不是非常方便,因此java提供了轉(zhuǎn)換流
字符流=字節(jié)流+編碼表
1)編碼表
由字符及其對應(yīng)的數(shù)值組成的一張表
圖解:
2)String類的編碼和解碼
String(byte[] bytes,String charsetName):
通過指定的字符集解碼字節(jié)數(shù)組
byte[]getBytes(String charsetName)
使用指定的字符串編碼為字節(jié)數(shù)組

String s="你好"; //編碼String-byte[] byte[] bys=s.getBytes();//無參默認(rèn)為gbk編碼,還可填"UTF-8"等 System.out.println(Arrays.toString(bys));//譯碼 String ss=new String(bys);bys后無參默認(rèn)gbk System.out.pintln(ss);

2)OutputStreamWriter
OutputStreamWriter(OutputStream out)
根據(jù)默認(rèn)編碼表,把字節(jié)流轉(zhuǎn)換成字符流
OutputStreamWriter(OutputStream out,String charsetName)
根據(jù)指定編碼表,把字節(jié)流轉(zhuǎn)換成字符流

//創(chuàng)建對象 OutputStreamWriter osw=new OutputStreamWriter(new FileOutStream("osw.txt")); //寫數(shù)據(jù) osw.write("你好"); osw.close();

3)InputStreamReader()
InputStreamReader(InputStream is)
用默認(rèn)的編碼讀取數(shù)據(jù)
InputStreamReader(InputStream is,String charsetName)
用指定的編碼讀取數(shù)據(jù)

InputStreamReader isr=new String InputStreamReader(new FileInputStream)(osw.txt)); int ch=0; while((ch=isr.read())!=-1){System.out.print((char) ch);isr.close();

4)5種寫數(shù)據(jù)方式

//創(chuàng)建對象 OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("osw2.txt") ) osw.write('a'); char chs={'a','b','c','d','e'}; ose.write(chs); ose.write(chs,1,3); osw.write("火狐"); osw.write("天天開心火狐"23); osw.flush();//刷新緩沖區(qū) osw.close();//也有刷新功能,但關(guān)閉后流對象不能繼續(xù)被使用

5)2種讀數(shù)據(jù)方法
InputStreamReader
int read();一次讀取一個字符
int read(char chs)l//一次讀取一個字符數(shù)組

InputStreamReader isr=new InputStreamReader(new FileInputStream());int ch=; while((ch=isr.read()0!=-1){System.out.println((char) ch); }char[] chs=new char[1024]; int len=0; while((len=isr.read(chs))!=-1){ System.out.print(new String(chs,0,len)); } isr.close();

@例題1:字符流復(fù)制文本文件
讀取數(shù)據(jù)-字符轉(zhuǎn)換流-InputStreamRreader

寫出數(shù)據(jù)-字符轉(zhuǎn)換流-OuputStreamRreader

InputStreamRreader isr=InputStreamRreader(new FileInputStream("a.txt")); OuputStreamWriter osw=new OuputStreamWriter (new FileOutputStream("b.txt")); //方式1 int ch=0; while((ch=isr.read()0!=-1){ osw.writer(ch); osw.close(); isr.close(); //方式2 char[] chs=new chat[1024]; int len=0; while((len=isr.read(chs))!=-1){ osw.write(chs,0,len); osw.fush(); } osw.close(); isr.close(); }

轉(zhuǎn)換流的名字較長,而我們常見的操作都是按照本地默認(rèn)的編碼實現(xiàn)的,所以,為了簡化我們的書寫,轉(zhuǎn)換流提供了對應(yīng)的子類
FileWriter:=FileOutputStream+編碼表(gbk)
FileReader:=FileInputStream+編碼表(gbk)

FileReader fr=new FileReader("a.txt");FileWriter fw=new FileWriter("b.txt"); while((len=isr.read(chs))!=-1){ osw.write(chs,0,len); osw.fush(); } fw.close(); fr.close(); } FileReader fr=new FileReader("a.txt");FileWriter fw=new FileWriter("b.txt"); int ch; while((ch=fr.read())!=-1){ fw.write(ch); } fw.close(); fr.close();

6)字符緩沖輸出(入)流
BufferedWriter
將文本寫入字符輸出流,緩沖各個字符,從而提供單個字符、數(shù)組和字符串的高效寫入,接收默認(rèn)值的大小。
BufferedReader
從字符輸入流讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的高效讀取。

寫數(shù)據(jù)

/* BufferedWriter bw=new BufferedWriter(new BufferedWriter(new FileOutputStream("bw.txt"))); *///復(fù)雜 BufferedWriter bw=new BufferedWriter(new FileWriter(bw.txt)); bw.write("hello"); bw.flush(); bw.close();

讀數(shù)據(jù)

BufferedReader br=new BufferedReader(new FileReader("bw.txt")); int ch=0; while((ch=br.read())!=-1{ System.out.print((char)ch);br.close();//方式2略

//字符緩沖流復(fù)制文件 BufferedReader br=new BufferedReader(new FileReader("a.txt")); BufferedWriter bw=new BufferedWriter(new FileWriter("b.txt")); char[] chs=new char[1024]; int len=0; while((len=br.read(chs))!=-1){ bw.write(chs,0,len); bw.flush(); ] bw.close(); br.close();

圖片和視頻不能使用字符流
7)字符緩沖流的特殊功能
public String readLine()
一次讀取一行數(shù)據(jù),不會讀取換行與回車

BufferedWriter bw=new BufferedWriter(new FileWriter("b2.txt")); for(int x=0;x<10;x++){ bw.write("hello"+x); bw.newLine();//換行 bw.flush(); } bw.close();//讀功能: public static void read(){ BufferedReader(new FileReader("bw2.txt")); String line=null; while((line=br.readerLine())!=null){ System.out.println(); } } //用上述特殊功能復(fù)制文件 BufferedReader br=new BufferedReader(new FileReader("a.txt")); BufferedReader bw=new BufferedWriter(new FileWriter("a.txt")); String line=null; while((ine=br.readLine())!=null{ bw.write(line); bw.newLine(); bw.flush(); } bw.close(); br.close();

8)總結(jié)

/*把集合中的數(shù)據(jù)存儲到文本文件 ArrayList集合里存儲的是字符串 遍歷ArrayList集合,把數(shù)據(jù)獲取到 然后存儲到文本文件中,使用字符流*/ArrayList<String>array=new Arrayist<String>(); array.add("a"); array.add("ab"); array.add("abv");BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt")); for(String s:array){ bw.write(s); bw.newLine(); bw.fush(); } bw.close(); } //將文本文件中的數(shù)據(jù)存儲到集合BufferedReader br=new BufferedReader(new FileReader("b.txt")); ArrayList<String>array=new Arrayist<String>(); String line=null; while((line=br.readLine())!=null){ array.add(line); }br.close(); for(String s:array){ System.out.println(s); } /*隨機(jī)獲取文本文件中的名字 -把文本文件中的數(shù)據(jù)存儲到集合中 -隨機(jī)產(chǎn)生一個索引 -根據(jù)該索引獲取一個值 */ BufferedReader br=new BufferedReader(new FileReader("b.txt")); ArrayList<String>array=new Arrayist<String>(); String line=null; while((line=br.readLine())!=null){ array.add(line); } br.close(); Random r=new Random(); int dex=r.nextInt(array.size()); String name=array.get(index); System.out.print("name"); //復(fù)制單級文件夾 /* -封裝目錄 -獲取該目錄下的所有文本文件的File數(shù)組 -遍歷該File數(shù)組,得到每一個File對象 -把該File進(jìn)行復(fù)制File srcFolder=new File("e:\\demo"); //目的文件夾若不存在將自動創(chuàng)建 File destFolder=new File("w:\\test"); if(!destFolder.exists()){ destFolder.mkdir(); } //獲取該目錄下所有文本的File數(shù)據(jù) File[] fileArray=srcFolder.listFiles(); 遍歷該File數(shù)組,得到每一個File對象 for(File file:fileArray){ String name=fie.getName(); File newFile=new File(destFolder,name); copyFile(file,newFile);private static void copyFile(File file,File newFile){ BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file)); byte[] bys=new byte[1024]; int len=0; while((len=bis.read(bys)!=-1){ bos.write(bys,0,len); } bos.close(); bis.c;ose(); } 數(shù)據(jù)源:e:\\java\\a.java 目的地:e:\\jad\\a.jad //復(fù)制指定目錄下指定后綴名的文件并修改文件名 public class CopyFolderDemo {public static void main(String[] args) throws IOException {// 封裝目錄File srcFolder = new File("e:\\java");// 封裝目的地File destFolder = new File("e:\\jad");// 如果目的地目錄不存在,就創(chuàng)建if (!destFolder.exists()) {destFolder.mkdir();}// 獲取該目錄下的java文件的File數(shù)組File[] fileArray = srcFolder.listFiles(new FilenameFilter() {@Overridepublic boolean accept(File dir, String name) {return new File(dir, name).isFile() && name.endsWith(".java");}});// 遍歷該File數(shù)組,得到每一個File對象for (File file : fileArray) {// System.out.println(file);// 數(shù)據(jù)源:e:\java\DataTypeDemo.java// 目的地:e:\\jad\DataTypeDemo.javaString name = file.getName();File newFile = new File(destFolder, name);copyFile(file, newFile);}// 在目的地目錄下改名File[] destFileArray = destFolder.listFiles();for (File destFile : destFileArray) {// System.out.println(destFile);// e:\jad\DataTypeDemo.java// e:\\jad\\DataTypeDemo.jadString name =destFile.getName(); //DataTypeDemo.javaString newName = name.replace(".java", ".jad");//DataTypeDemo.jadFile newFile = new File(destFolder,newName);destFile.renameTo(newFile);}}private static void copyFile(File file, File newFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));byte[] bys = new byte[1024];int len = 0;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();} } //復(fù)制多級文件夾 -封裝數(shù)據(jù)源目錄 -封裝目的地目錄 -判斷該file是文件還是文件夾,若是文件,直接復(fù)制;若是文件夾,就在目的地目錄下創(chuàng)建該文件夾,并獲取該File對象下的所有文件或者文件夾File對象,遍歷得到每一個File對象,在該目錄下重復(fù)上述操作//封裝數(shù)據(jù)源目錄 File srcFile=new File("E:\\JavaSE\\day21"); -封裝目的地目錄 File destFile=new File("E:\\");//復(fù)制文件夾的方法 copyFolder(srcFile,destFile); private static void copyFolder(File srcFile,File destFile){ if(srcFile.isDirectory()){ //文件夾 File newFolder=new File(destFile,srcFile.getName()); newFolder.mkdir();//獲取該File對象下的所有文件或者文件夾File對象 File[] fileArray=srcfile.listFiles(); for(File file:fileArray){ copyFolder(null,newFolder); } }else{ //文件 File newFile=new File(destFile.srcFile.getName()); copyFile(srcFile,destFile); } } //復(fù)制文件的方法 private static void copyFile(File srcFile,File newFile){ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcfile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));byte[] bys = new byte[1024];int len = 0;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();} } //鍵盤錄入學(xué)生信息按照總分排序并寫入文本文件//Student類 public class Student{ private String name; private int math; private int Chinese; private int English; //其余略}//創(chuàng)建集合對象 TreeSet<Student>ts=new TreeSet<Student>(new Comparator<Student>(){ public int compare(Student s1,Student s2){ int num=s2.getSum()-s1.getSum(); int num2=num==0?s1.getChinese()-s2.getChinese():num; int num3=num2==0?s1.getMath(0-s2.getMath:num2; int num4=num3==0?s1.getEnglish(0-s2.getEnglish:num2; int num5=num4==0?s1.getName().compareTo(s1.getName()):num4; return num5; } }); //鍵盤錄入學(xué)生信息到集合 for(int x=1;x<=5;x++){ //略 } Student s=new Student(); s.setName(Name); s.setName(Chinese); s.setName(math); s.setName(English);//把學(xué)生信息添加到集合 ts.add(s); //遍歷集合,把數(shù)據(jù)寫到文本文件中 BufferedWriter bw=new BufferedWriter(new FileWriter("Students.txt")); bw.write("學(xué)生信息如下"); bw.newLine(); bw.flush(); bw.write("姓名,語文成績,英語成績"); bw.newLine(); bw.flush(); for(Student s:ts){ StringBuilder sb=new StringBuilder(); sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getEnglish());bw.write(sb.toString()); bw.newLine(); bw.flush(); } //釋放資源 bw.close() }

@例題5:

分析:* A:把s.txt這個文件給做出來* B:讀取該文件的內(nèi)容,存儲到一個字符串中* C:把字符串轉(zhuǎn)換為字符數(shù)組* D:對字符數(shù)組進(jìn)行排序* E:把排序后的字符數(shù)組轉(zhuǎn)換為字符串* F:把字符串再次寫入ss.txt中*/ public class StringDemo {public static void main(String[] args) throws IOException {// 讀取該文件的內(nèi)容,存儲到一個字符串中BufferedReader br = new BufferedReader(new FileReader("s.txt"));String line = br.readLine();br.close();// 把字符串轉(zhuǎn)換為字符數(shù)組char[] chs = line.toCharArray();// 對字符數(shù)組進(jìn)行排序Arrays.sort(chs);// 把排序后的字符數(shù)組轉(zhuǎn)換為字符串String s = new String(chs);// 把字符串再次寫入ss.txt中BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));bw.write(s);bw.newLine();bw.flush();bw.close();} }

//登錄注冊IO版

//這是用戶操作的具體實現(xiàn)類public class UserDaoImpl implements private static File file=new File("user.txt") static{ try{ file.createNewFile(); }catch(IOException e){ System.out.println("創(chuàng)建文件失敗") } } UserDao{ boolean flag=false; public boolean isLogin(String username,String password){ return false; } public void regist(User user){ BufferedWriter bw=null; try{ br=new BufferedReader(new FileReader("user.txt")); String line=null; while((line=br.readLine())!=null){ String[] datas=line.split("="); if(data[0].equals(username)&&datas[1].equals(password)){ falg=true; break; } }catch(FileNOtFoundException e){ System.out.println("登錄找不到信息所在文件"); }catch(IOException e){ System.out.println("登錄失敗"); } return false;} try{ bw=new BufferedWriter(new FileWriter("wser.txt")); bw.write(user.getUsername()+"="+user.getPassword()); bw.newLine(); bw.Flush(); }catch(IOException e){ e.printStackTrace(); }finally{if(bw!=null))try{ bw.close(); } catch(IOException e){}finally{ if(br!=null){ try{ br.close(); }catch(IOException e){ System.out.println(內(nèi)存釋放失敗); } } } } } return flag; }

轉(zhuǎn)載于:https://www.cnblogs.com/Tanqurey/p/10485330.html

總結(jié)

以上是生活随笔為你收集整理的JAVA自学笔记21的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。