首页 > 编程语言 >java IO流

java IO流

时间:2022-11-06 23:00:16浏览次数:57  
标签:java IO File System String file println out

java io流详解:

文件

1、什么是文件?

文件是我们保存数据的地方。

2、文件流

文件在程序中是以流的形式来操作的。

流:数据在数据源(文件)和程序(内存)之间经历的路径

输入流:数据从数据源(文件)到程序(内存)的路径

输出流:数据从程序(内存)到数据源(文件)的路径

 

3.字符流和字节流

字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。字节流和字符流的区别:

(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。

(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件,我们将在下面验证这一点。

结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

4.输入流和输出流

对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。

Java流类图结构:

 

5、常用的文件操作

1、创建文件对象相关构造器和方法

new File(String pathname) 

new File(File parent,String child) 

new File(String parent,String child) 

createNewFile()

import org.testng.annotations.Test;

import java.io.File;

import java.io.IOException;

 

public class FileCreate {

    public static void main(String[] args) {

 

    }

    @Test

    public void create1(){

        String filePath = "D:\\file1.txt";

        File file = new File(filePath);

        try {

            file.createNewFile();

            System.out.println("创建文件1成功");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

2、获取文件的相关信息

import org.testng.annotations.Test;

import java.io.File;

public class FileInformation {

    public static void main(String[] args) {

    }

    @Test

    public void Info(){

        File file = new File("D:\\file1.txt");

        System.out.println("文件名称:"+file.getName());

        System.out.println("文件绝对路径:"+file.getAbsolutePath());

        System.out.println("文件父目录:"+file.getParent());

        System.out.println("文件大小(字节):"+file.length());

        System.out.println("文件是否存在:"+file.exists());

        System.out.println("是否是文件:"+file.isFile());

        System.out.println("是否是目录:"+file.isDirectory());

    } 

}

 

3、目录的操作

import org.testng.annotations.Test;

import java.io.*;

public class fileDirectory {

    public static void main(String[] args) {

 

    }

    @Test

    public void fileDelete(){

        String filePath = "D:\\file1.txt";

        File file = new File(filePath);

 

        if(file.exists()){

            if(file.delete()){

                System.out.println(filePath+"删除成功");

            }else {

                System.out.println(filePath+"删除失败");

            };

        }else{

            System.out.println("文件不存在");

        }

 

    }

    @Test

    public void fileDeleteD(){

        String filePath = "D:\\file1.txt";

        File file = new File(filePath);

        if(file.exists()){

            if(file.delete()){

                System.out.println(filePath+"删除成功");

            }else {

                System.out.println(filePath+"删除失败");

            };

        }else{

            System.out.println("目录不存在");

        }

    }

    @Test

    public void fileDeleteD1(){

        String dirPath = "D:\\test\\dir1.txt";

        File file = new File(dirPath);

 

        if(file.exists()){

            System.out.println(dirPath+"该目录已经存在");

        }else{

            if(file.mkdirs()){

                System.out.println("创建成功");

            }else {

                System.out.println("创建失败");

            };

        }

 

    }

    //InputStream

    //OutputStream

    //Writer

    //Reader

}

6、Scanner与Println

1、基本键盘输入

import org.testng.annotations.Test;

import java.util.Scanner;

public class scanPrintTest {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String str = input.next();

        System.out.println(str);

        System.out.println("hello world");

    }

}

2、常见键盘输入类型

import java.util.Scanner;

public class scanTest {

    public static void main(String[] args) {

        Scanner input =new Scanner(System.in);

        System.out.print("请输入一个double类型的数:");

        double d = input.nextDouble();

        System.out.println(d);

        System.out.print("请输入一个int类型的数:");

        int i = input.nextInt();

        System.out.println(i);

        System.out.print("请输入一个string类型的数:");

        String s = input.next();

        System.out.println(s);

    }

}

标签:java,IO,File,System,String,file,println,out
From: https://www.cnblogs.com/maijiehui/p/16864523.html

相关文章

  • javaIo流笔记
    一、文件创建 importorg.junit.Test;importjava.io.File;importjava.io.IOException;publicclassFileCreate{publicstaticvoidmain(String[]args){......
  • JavaIO流
    常用的文件操作1.newfile(stringpathname)newfile(fileparent,stringchild)newfile(stringparent,stingchild) creatNewFlie() importorg.testng.annot......
  • IO流
    1、IO流原理及流的分类1.1、JavaIO原理I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通信等。Java程序中,对于数据的......
  • 论文笔记 - PRISM: A Rich Class of Parameterized Submodular Information Measures
    Motivation与ActiveLearning类似,TargetLearning致力于挑选外卖更“感兴趣”的数据,即人为为更重要的数据添加bias。例如我们当前的任务目标是增强自动驾驶算法的夜......
  • javaIO流
    IO流创建文件publicvoidcreate1(){StringfilePath="D:\\file1.txt.";Filefile=newFile(filePath);try{file.createNewFile();System.out.println("创建文件1成功");......
  • Java_Io
    1.创建文件对象相关构造器和方法newFile(Stringpathname)//根据路径构建一个File对象publicvoidcreate1(){StringfilePath="C:\\Users\\cen\\Desktop\\Tc......
  • java反射基础
    一,概念JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方......
  • 推荐Visual Studio四款好用插件
    我要推荐的4个插件,合理使用可以提高工作效率,分别是:1.MarkdownEditor可以在vs预览markdown文件的插件2.AddNewFile我们原本在vs中新建文件,需要添加新建项,再选想要的......
  • JavaIO流一部分代码实现
    创建文件代码importjava.io.File;importjava.io.IOException;publicclassMain{publicstaticvoidmain(String[]args){//writeyourcodehere......
  • 使用selenium-java报错:java.lang.NoSuchMethodError: com.google.common.base.Precond
    引入selenium-java依赖,发现到WebDriverdriver=newChromeDriver()时报错解决:<dependency><groupId>com.google.guava</groupId><a......