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)換成字符流
OutputStreamWriter osw=
new OutputStreamWriter(
new FileOutStream(
"osw.txt"));
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(
"天天開心火狐",
2,
3);
osw
.flush()
osw
.close()
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"));
int ch
=0;
while((ch=isr.read(
)0!
=-1){
osw.writer(ch);
osw.
close();
isr.
close();
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 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();
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();
}
}
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é)
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();
} 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");
/*
-封裝目錄
-獲取該目錄下的所有文本文件的File數(shù)組
-遍歷該File數(shù)組,得到每一個File對象
-把該File進(jìn)行復(fù)制File srcFolder=
new File(
"e:\\demo");
File destFolder=
new File(
"w:\\test");
if(!destFolder.exists()){
destFolder.mkdir();
}
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
public class CopyFolderDemo {public static void main(String[] args)
throws IOException {File srcFolder =
new File(
"e:\\java");File destFolder =
new File(
"e:\\jad");
if (!destFolder.exists()) {destFolder.mkdir();}File[] fileArray = srcFolder.listFiles(
new FilenameFilter() {
@Overridepublic boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(
".java");}});
for (File file : fileArray) {String name = file.getName();File newFile =
new File(destFolder, name);copyFile(file, newFile);}File[] destFileArray = destFolder.listFiles();
for (File destFile : destFileArray) {String name =destFile.getName(); String newName = name.replace(
".java",
".jad");File 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();}
}
-封裝數(shù)據(jù)源目錄
-封裝目的地目錄
-判斷該file是文件還是文件夾,若是文件,直接復(fù)制;若是文件夾,就在目的地目錄下創(chuàng)建該文件夾,并獲取該File對象下的所有文件或者文件夾File對象,遍歷得到每一個File對象,在該目錄下重復(fù)上述操作
File srcFile=
new File(
"E:\\JavaSE\\day21");
-封裝目的地目錄
File destFile=
new File(
"E:\\");
copyFolder(srcFile,destFile);
private static void copyFolder(File srcFile,File destFile){
if(srcFile.isDirectory()){
File newFolder=
new File(destFile,srcFile.getName());
newFolder.mkdir();
File[] fileArray=srcfile.listFiles();
for(File file:fileArray){
copyFolder(
null,newFolder);
}
}
else{
File newFile=
new File(destFile.srcFile.getName());
copyFile(srcFile,destFile);
}
}
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();}
}
public class Student{
private String name;
private int math;
private int Chinese;
private int English;
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;
}
});
for(
int x=
1;x<=
5;x++){
}
Student s=
new Student();
s.setName(Name);
s.setName(Chinese);
s.setName(math);
s.setName(English);
ts.add(s);
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 {BufferedReader br =
new BufferedReader(
new FileReader(
"s.txt"));String line = br.readLine();br.close();
char[] chs = line.toCharArray();Arrays.sort(chs);String s =
new String(chs);BufferedWriter bw =
new BufferedWriter(
new FileWriter(
"ss.txt"));bw.write(s);bw.newLine();bw.flush();bw.close();}
}
//登錄注冊IO版
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)容還不錯,歡迎將生活随笔推薦給好友。