????????(1)无参数的read()方法返回值为 int ?类型,表示?返回获取值的哈希码
????????(2)有参数的read(byte[]?bs)方法的返回值表示?返回实际读取个数 ,参数表示?读取的值哈希码存入数组中
?
A. java?源文件??字符流 B. .class?字节码文件???字节流
C. .html?网页文件???字符流 D. .jpg?图像文件???字节流
E. .mp3?音乐文件???字节流 F. .txt?文件???字符流
try(FileInputStream fin=new FileInputStream("test.txt")){
????System.out.println(fin.read());
}catch (Exception e){
????
}
? ? ? ? (1)?用FileOutputStream?往当前目录下“test.txt”文件中写入“Hello?World”;
? ? ? ? (2)利用FileInputStream?读入test.txt 文件,并在控制台上打印出test.txt?中的内容。
? ? ? ? (3)要求用 try-catch-finally?处理异常,并且关闭流应放在 finally?块中。
package com.by.homework7;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import javax.imageio.IIOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLOutput;
public class Test7 {
????public static void main(String[] args) {
????????FileOutputStream fo=null;
????????FileInputStream fi=null;
???????try{
???????????fo=new FileOutputStream("file/test.txt");
???????????fi=new FileInputStream("file/test.txt");
????????????String str="Hello World";
????????????fo.write(str.getBytes());
????????????fo.flush();
????????????while(true) {
????????????????int n=fi.read();
????????????????if (n==-1){
????????????????????break;
????????????????}
????????????????System.out.println(n);
????????????}
???????}catch (FileNotFoundException e){
???????????System.out.println("文件路径有误");
???????}catch (IOException e){
???????????System.out.println("文件读写有误");
???????}catch (Exception e){
???????????System.out.println("未知错误");
???????}finally {
???????????if (fi!=null) {
???????????????try {
???????????????????fi.close();
???????????????} catch (IOException e) {
???????????????????throw new RuntimeException(e);
???????????????}
???????????}
???????????if (fo!=null){
???????????????try {
???????????????????fo.close();
???????????????} catch (IOException e) {
???????????????????throw new RuntimeException(e);
???????????????}
???????????}
???????}
????}
}
? ? ? ? (1)将 26?个大写字母(A~Z)写入到当前项目a.txt?文件中
? ? ? ? (2)?读取文件中的内容,将读取的内容连接为一个字符串,并将所有的大写字母转变为小写字母打印输出转换的结果
package com.by.homework7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test8 {
????public static void main(String[] args) {
????????FileOutputStream fo=null;
????????FileInputStream fi=null;
????????try{
????????????fo=new FileOutputStream("file/a.txt");
????????????fi=new FileInputStream("file/a.txt");
????????????String str="ABCDEFGHIGKLMNOPQRSTUVWXYZ";
????????????fo.write(str.getBytes());
????????????fo.flush();
????????????while(true) {
????????????????int n=fi.read();
????????????????if (n==-1){
????????????????????break;
????????????????}
????????????????char c= (char) n;
????????????????String s= String.valueOf(c);
????????????????System.out.print(s.toLowerCase());
????????????}
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}finally {
????????????if (fi!=null) {
????????????????try {
????????????????????fi.close();
????????????????} catch (IOException e) {
????????????????????throw new RuntimeException(e);
????????????????}
????????????}
????????????if (fo!=null){
????????????????try {
????????????????????fo.close();
????????????????} catch (IOException e) {
????????????????????throw new RuntimeException(e);
????????????????}
????????????}
????????}
????}
}
"C:\Program Files\Java\jdk1.8.0_341\bin\java.exe" "-javaagent:C:\Program com.by.homework7.Test8
abcdefghigklmnopqrstuvwxyz
package com.by.homework7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Test10 {
????public static void main(String[] args) {
????????Set<String> set=new HashSet<>();
????????Set<String> set1=new TreeSet<>();
????????try(FileOutputStream fo=new FileOutputStream("file/d.txt");
????????????????FileInputStream fi=new FileInputStream("file/c.txt")
????????){
?????????????while (true){
?????????????????int n=fi.read();
?????????????????if (n==-1){
?????????????????????break;
?????????????????}
?????????????????char c= (char) n;
?????????????????String str= String.valueOf(c);
?????????????????set.add(str);
?????????????}
????????????set1.addAll(set);
????????????String str2= set1.toString();
????????????/*for (String s:set1
?????????????????) {
????????????????bytes=s.getBytes();
????????????}*/
????????????byte[] bytes = str2.getBytes();
????????????fo.write(bytes);
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}
// ???????set.forEach(string-> System.out.println(string));
// ???????set1.addAll(set);
????????set1.forEach(string-> System.out.print(string));
????}
}
package com.by.homework7;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test11 {
????public static void main(String[] args) {
????????try(FileOutputStream fo=new FileOutputStream("file/a1.txt");){
????????????String str="abcdefghijklmnopqrstuvwxyz";
????????????byte[] bs=str.getBytes();
????????????fo.write(bs);
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}
????}
}
package com.by.homework7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test11 {
????public static void main(String[] args) {
????????try(FileOutputStream fo=new FileOutputStream("file/a1.txt");
????????FileInputStream fi=new FileInputStream("file/a1.txt");){
????????????String str="abcdefghijklmnopqrstuvwxyz";
????????????byte[] bs=str.getBytes();
????????????fo.write(bs);
????????????fo.flush();
????????????while (true){
????????????????int n=fi.read();
????????????????if (n==-1){
????????????????????break;
????????????????}
????????????????System.out.println(n);
????????????}
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}
????}
}
?package com.by.homework7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test13 {
????public static void main(String[] args) {
????????try(FileOutputStream fo=new FileOutputStream("file/b1.txt");
????????????FileInputStream fi=new FileInputStream("file/a1.txt");){
/* ???????????String str="abcdefghijklmnopqrstuvwxyz";
????????????byte[] bs=str.getBytes();
????????????fo.write(bs);
????????????fo.flush();*/
????????????while (true){
????????????????int n=fi.read();
????????????????if (n==-1){
????????????????????break;
????????????????}
????????????????System.out.println(n);
????????????????fo.write(n);
????????????}
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}
????}
}
package com.by.homework7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test14 {
????public static void main(String[] args) {
????????FileOutputStream fo=null;
????????FileInputStream fi=null;
????????try{
????????????fo=new FileOutputStream("file/b1.txt");
????????????fi=new FileInputStream("file/a1.txt");
/* ???????????String str="abcdefghijklmnopqrstuvwxyz";
????????????byte[] bs=str.getBytes();
????????????fo.write(bs);
????????????fo.flush();*/
????????????while (true){
????????????????int n=fi.read();
????????????????if (n==-1){
????????????????????break;
????????????????}
????????????????System.out.println(n);
????????????????fo.write(n);
????????????}
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}finally {
????????????if (fi!=null){
????????????????try {
????????????????????fi.close();
????????????????} catch (IOException e) {
????????????????????throw new RuntimeException(e);
????????????????}
????????????}
????????????if (fo!=null){
????????????????try {
????????????????????fo.close();
????????????????} catch (IOException e) {
????????????????????throw new RuntimeException(e);
????????????????}
????????????}
????????}
????}
}
package com.by.homework7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test8 {
????public static void main(String[] args) {
????????FileOutputStream fo=null;
????????FileInputStream fi=null;
????????try{
????????????fo=new FileOutputStream("file/a.txt");
????????????fi=new FileInputStream("file/a.txt");
????????????String str="ABCDEFGHIGKLMNOPQRSTUVWXYZ";
????????????fo.write(str.getBytes());
????????????fo.flush();
????????????while(true) {
????????????????int n=fi.read();
????????????????if (n==-1){
????????????????????break;
????????????????}
????????????????char c= (char) n;
????????????????String s= String.valueOf(c);
????????????????System.out.print(s.toLowerCase());
??????????????/* ?int num=n+25;
????????????????char c=(char)num;
????????????????String s=String.valueOf(c);
????????????????System.out.print(s);*/
????????????}
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}finally {
????????????if (fi!=null) {
????????????????try {
????????????????????fi.close();
????????????????} catch (IOException e) {
????????????????????throw new RuntimeException(e);
????????????????}
????????????}
????????????if (fo!=null){
????????????????try {
????????????????????fo.close();
????????????????} catch (IOException e) {
????????????????????throw new RuntimeException(e);
????????????????}
????????????}
????????}
????}
}
package com.by.homework7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test16 {
????public static void main(String[] args) {
????????try(
????????????????FileOutputStream fo=new FileOutputStream("file/girl.jpg");
????????????????FileInputStream fi=new FileInputStream("C:\\Users\\user\\Desktop\\java学习题\\IO1练习题-无对象过滤流/gril.jpg");
????????????????){
????????????while (true){
????????????????int n=fi.read();
????????????????if (n==-1){
????????????????????break;
????????????????}
????????????????fo.write(n);
????????????}
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}
????}
}
?package com.by.homework7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test17 {
????public static void main(String[] args) {
????????try(
????????????????FileOutputStream fo=new FileOutputStream("file/1.mp3");
????????????????FileInputStream fi=new FileInputStream("C:\\Users\\user\\Desktop\\java学习题\\IO1练习题-无对象过滤流/Corporate.mp3");
????????){
????????????while (true){
????????????????int n=fi.read();
????????????????if (n==-1){
????????????????????break;
????????????????}
????????????????fo.write(n);
????????????}
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}
????}
}
package com.by.homework7;
import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Test10 {
????public static void main(String[] args) {
????????Set<String> set=new HashSet<>();
????????Set<String> set1=new TreeSet<>();
????????try(
????????????????BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("file/d.txt"));
????????????????BufferedInputStream bis=new BufferedInputStream(new FileInputStream("file/c.txt"));
???????????????/* FileOutputStream fo=new FileOutputStream("file/d.txt");
????????????????FileInputStream fi=new FileInputStream("file/c.txt");*/
????????){
?????????????while (true){
?????????????????int n=bis.read();
?????????????????if (n==-1){
?????????????????????break;
?????????????????}
?????????????????char c= (char) n;
?????????????????String str= String.valueOf(c);
?????????????????set.add(str);
?????????????}
????????????set1.addAll(set);
????????????String str2= set1.toString();
????????????/*for (String s:set1
?????????????????) {
????????????????bytes=s.getBytes();
????????????}*/
????????????byte[] bytes = str2.getBytes();
????????????bos.write(bytes);
????????}catch (FileNotFoundException e){
????????????System.out.println("文件路径有误");
????????}catch (IOException e){
????????????System.out.println("文件读写有误");
????????}catch (Exception e){
????????????System.out.println("未知错误");
????????}
// ???????set.forEach(string-> System.out.println(string));
// ???????set1.addAll(set);
????????set1.forEach(string-> System.out.print(string));
????}
}
?