image.png

  1. import org.junit.Test;
  2. import java.io.File;
  3. import java.io.IOException;
  4. public class iofile {
  5. public static void main(String[] args) {
  6. }
  7. @Test
  8. public void creat01(){
  9. File parentFile=new File("d:\\");
  10. String fileName="news1.text";
  11. File file = new File(parentFile,fileName);
  12. try {
  13. file.createNewFile();
  14. System.out.println("创建成功");
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. @Test
  20. public void creat02(){
  21. String filePath="d:\\new2.txt";
  22. File file = new File(filePath);
  23. try {
  24. file.createNewFile();
  25. System.out.println("文件创建成功");
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. @Test
  31. public void creat03(){
  32. String parentFile="d:\\";
  33. String fileName="text03.txt";
  34. File file = new File(parentFile,fileName);
  35. try {
  36. file.createNewFile();
  37. System.out.println("文件创建成功!");
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }

image.png
image.png
image.png
image.pngimage.png
image.png
image.png
image.png

csdn上面的讲解

链接
image.png

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class IoTest {
  6. //字节流
  7. public static void main(String[] args) throws IOException {
  8. File file1 = new File("D:/test1.txt");
  9. write(file1);
  10. System.out.println(read(file1));
  11. }
  12. public static void write(File file)throws IOException {
  13. FileOutputStream os = new FileOutputStream(file,true);
  14. String s="人家最喜欢惠祁啦!";
  15. os.write(s.getBytes());
  16. os.close();
  17. }
  18. public static String read(File file)throws IOException{
  19. FileInputStream fs = new FileInputStream(file);
  20. byte[]bytes=new byte[1024];
  21. StringBuilder sb = new StringBuilder();
  22. int length;
  23. while ((length=fs.read(bytes))!=-1){
  24. sb.append(new String(bytes,0,length));
  25. }
  26. fs.close();
  27. return sb.toString();
  28. }
  29. }
  1. //缓冲字节流
  2. public class IOTest01 {
  3. public static void main(String[] args) throws IOException {
  4. File file = new File("D:/test2.txt");
  5. write(file);
  6. System.out.println(read(file));
  7. }
  8. public static void write(File file)throws IOException {
  9. BufferedOutputStream bs = new BufferedOutputStream(new FileOutputStream(file,true));
  10. String s="垃圾惠祁垃圾惠祁!";
  11. bs.write(s.getBytes());
  12. bs.close();
  13. }
  14. public static String read(File file)throws IOException{
  15. BufferedInputStream fs = new BufferedInputStream(new FileInputStream(file));
  16. byte [] bytes=new byte[1024];
  17. //用来接收读取的字节数组
  18. StringBuilder sb=new StringBuilder();
  19. //读取到字节数组的长度
  20. int length;
  21. while ((length=fs.read(bytes))!= -1){
  22. //将读取到的内容转化为字符串
  23. sb.append(new String(bytes,0,length));
  24. }
  25. fs.close();
  26. return sb.toString();
  27. }
  28. }
  1. //字符流
  2. public class IOTest03 {
  3. public static void main(String[] args)throws IOException {
  4. File file = new File("D:/test03.txt");
  5. write(file);
  6. System.out.println(read(file));
  7. }
  8. public static void write(File file) throws IOException {
  9. OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file, true));
  10. String s = "io流好麻烦呀1";
  11. ow.write(s);
  12. ow.close();
  13. }
  14. public static String read(File file)throws IOException{
  15. InputStreamReader ir = new InputStreamReader(new FileInputStream(file), "UTF-8");
  16. //一起读取多少给字符
  17. char[]chars=new char[1024];
  18. //读取的字符数组先append到StringBuilder里面
  19. StringBuilder sb=new StringBuilder();
  20. int length;
  21. while ((length= ir.read(chars))!= -1){
  22. sb.append(new String(chars,0,length));
  23. }
  24. ir.close();
  25. return sb.toString();
  26. }
  27. }
  28. import java.io.*;
  29. import java.nio.charset.StandardCharsets;
  30. //字符流
  31. public class IOTest03 {
  32. public static void main(String[] args)throws IOException {
  33. File file = new File("D:/test03.txt");
  34. write(file);
  35. System.out.println(read(file));
  36. }
  37. public static void write(File file) throws IOException {
  38. OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file, true));
  39. String s = "io流好麻烦呀1";
  40. ow.write(s);
  41. ow.close();
  42. }
  43. public static String read(File file)throws IOException{
  44. InputStreamReader ir = new InputStreamReader(new FileInputStream(file), "UTF-8");
  45. //一起读取多少给字符
  46. char[]chars=new char[1024];
  47. //读取的字符数组先append到StringBuilder里面
  48. int length;
  49. while ((length= ir.read(chars))!= -1){
  50. System.out.println(new String(chars,0,length));
  51. }
  52. ir.close();
  53. return new String();
  54. }
  1. ///字符流便捷类
  2. public class IOTest02 {
  3. public static void main(String[] args)throws IOException {
  4. File file = new File("D:/test04.txt");
  5. write(file);
  6. System.out.println(read(file));
  7. }
  8. public static void write(File file)throws IOException {
  9. FileWriter fw = new FileWriter(file, true);
  10. String s="慢慢学一定可以的!";
  11. fw.write(s);
  12. fw.close();
  13. }
  14. public static String read(File file)throws IOException{
  15. FileReader fileReader = new FileReader(file);
  16. char [] chars= new char[1024];
  17. StringBuilder sb=new StringBuilder();
  18. int length;
  19. while ((length=fileReader.read(chars)) != -1){
  20. sb.append(new String(chars,0,length));
  21. }
  22. fileReader.close();
  23. return sb.toString();
  24. }
  25. }

文件的拷贝

  1. import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
  2. import java.io.*;
  3. //文件拷贝
  4. public class IOTest04 {
  5. public static void main(String[] args) throws IOException {
  6. {
  7. try {
  8. FileInputStream fi = new FileInputStream("D:/test2.txt");
  9. FileOutputStream fo = new FileOutputStream("D:/test03.txt",true);
  10. byte[] bytes = new byte[1024];
  11. StringBuilder stringBuilder = new StringBuilder();
  12. int length;
  13. while ((length = fi.read(bytes)) != -1) {
  14. fo.write(bytes, 0, length);
  15. }
  16. fi.close();
  17. fo.close();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }
  23. }

image.png
image.png