首页 > 编程语言 >JAVAI学习笔记

JAVAI学习笔记

时间:2022-11-05 19:23:21浏览次数:34  
标签:文件 String JAVAI 笔记 学习 File import new public

文件流

什么是文件流?
数据从一个地方流到另一个地方

可读流(Readable):外部设备(磁盘,网卡,显卡,打印机等等) --->>> 内存

可写流(Writeable):内存 --->>> 外部设备(磁盘,网卡,显卡,打印机等等)

双工流(Duple):内存 <<<---->>> 外部设备(磁盘,网卡,显卡,打印机等等)


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


3、常用的文件操作
1、创建文件对象相关构造器和方法
new File(String pathname)//根据路径构建一个File对象
new File(File parent,String child)//根据父目录文件+子路径构建new File(String parent,String child)//根据父目录+子路径构建
createNewEile()//创建文件

  第一种构建文件的方法

,具体代码如下:

import org.testng.annotations.Test;

import java.io.File;

import java.io.IOException;

public class testC {

    public static void main(String[] args){

    }

    @Test

    public void createFile(){

        String filePath ="d:/file1.txt";

        File file = new File(filePath);

        try{

            file.createNewFile();

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

        } catch(IOException e){

            e.printStackTrace();

        }

    }

}

得到结果截图如下:

 第二种构建方法

代码如下:

import org.testng.annotations.Test;

import java.io.File;

import java.io.IOException;

public class testC2 {

    public static void main(String[] args){

    }

    @Test

    public void create20(){

        File parentFile = new File("D:\\");

        String fileNane = "file2.txt";

        File file = new File(parentFile,fileNane);

        try{

            file.createNewFile();

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

        } catch(IOException e){

           throw new RuntimeException(e);

        }

    }

}

得到结果截图:

 

 

第三种构建方法

代码如下:

import org.testng.annotations.Test;

import java.io.File;

import java.io.IOException;

public class testC3 {

    public static void main(String[] args){

}

    @Test

    public void create30(){

        String parentPath = "d:\\";

        String filePath = "file3.txt";

        File file = new File(parentPath,filePath);

        try{

            file.createNewFile();

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

        } catch(IOException e){

            throw new RuntimeException(e);

        }

    }

输入(Scanner)

截图:

 

 

Scanner 类

 最常用的四个函数

hasNext();

next();

hasNextLine();

nextLine();

其他常用函数
nextBoolean()和hasNextBoolean()

nextByte()和hasNextByte()

nextShort()和hasNextShort()

nextInt()和hasNextInt()

nextLong和hasNextLong()

nextFloat()和hasNextFloat()

nextDouble和hasNextDouble()

标签:文件,String,JAVAI,笔记,学习,File,import,new,public
From: https://www.cnblogs.com/Artaxias/p/16860885.html

相关文章

  • 2022-2023-1 20221406 《计算机基础与程序设计》第九周学习总结
    2022-2023-120221401《计算机基础与程序设计》第九周学习总结作业信息班级链接 https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业要求 https://www.cnblog......
  • JavaIO流
    文件的创建与查询1、什么是文件?文件是我们保存数据的地方2、文件流文件在程序中是以流的形式来操作的。流:数据在数据源(文件)和程序(内存)之间经历的路径输入流:数据从数据......
  • 学习笔记——自增长的键值问题、批处理
    2022-11-05 一、自增长的键值问题1、说明:将在数据库表单中添加数据的自增长的字段返回给用户2、使用方式:在预编译语句中,除了要传入sql语句外,还要传入一个参......
  • PyTorch笔记:Modules官方文档
    来自https://pytorch.org/docs/stable/notes/modules.htmlASimpleCustomModuleimporttorchfromtorchimportnnclassMyLinear(nn.Module):def__init__(se......
  • 【单片机/嵌入式】【梁山派】学习日志07:位带操作
    位带操作一、位带操作介绍为了减少“读-改-写”操作的次数,Cortex-M4处理器提供了一个可以执行单原子比特操作的位带功能。存储器映射包含了两个支持位带操作的区域。其中......
  • 数字逻辑笔记 全加器全减器8421BCD转余3
    二进制全加器全减器十进制加法8421BCD转余3码......
  • webpack5 学习手册
    1.初始化webpack项目npminit-y(初始化一个package.json文件)2.npmiwebpackwebpack-cli-D下载这两个依赖包3.npxwebpack./src/main.js--modedevelopme......
  • 学习笔记
    JavaScript学习BOM操作BOM是浏览器对象模型(BrowserObjectModel)。它使JavaScript有能力与浏览器进行“对话”。alert():警告弹窗confirm():确定弹窗prompt():提示弹框......
  • 【漏洞学习及复现】Log4j2(CVE-2021-44228)漏洞复现
    Log4j2漏洞(CVE-2021-44228)Log4j2ApacheLog4j2是一个基于Java的日志记录工具。该工具重写了Log4j框架,并且引入了大量丰富的特性。该日志框架被大量用于业务系统开......
  • shell-函数学习笔记二
    shell函数的定义#方法一functionname{command...command}#方法二name(){command...command}函数的调用直接使用函数名调用,可以将函数......