JavaIO流

常用的文件操作


1.

new file(string pathname)

new file(file parent,string child)

new file(string parent,sting child)

 

creatNewFlie()

 

import org.testng.annotations.Test;

 

import java.io.File;

import java.io.IOException;

 

public class FileCreate{

  public static viod main(string[] args){

  }

//方法一

@Test

public void create1(){

string filepath = "D:\\file1.txt";

File fiel = new file(filepath);

Try{

File.createNewfile();

System.out.printl(创建文件1111成功咯!)

}catch(IOException e){

e.prinstacktrace();

}

}

//方式2

@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(OException e) {

throw new RuntimeException(e);

}

}

 

//方式三

@Test

public void create3({

String parentPath="d:\\;

String filePath="file3.txt";

 

File file =new File(parentPath,filePath);

 

try {

file.createNewFile0;

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

 

}catch (1OException e){

throw new RuntimeException(e);

 

2、获取文件的相关信息

常见的 File 相关方法

getName()/getAbsolutePath/getParent/length/exists/isFile/isDirectory

获取文件名、获取绝对路径

UTF-8一个英文一个字节,一个汉字三个字节

import org.testng.annotations.Test;

import java.io.File;

 

public class Filelnformation{

public static void main(Stringl args){

 

//获取文件信息

@Test

public void lnfo{

//先创建文件对象

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

 

//调用相应方法,得到对应信息

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

 

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

System.outprintln(”文件父目录:"+file.getParent();

 

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

 

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

 

System.out.printn("是否是文件:"+file.isFile0);

 

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

 

}

 

}

3目录的操作

创建一级目录:mkdir,创建多级目录:mkdirsdelete删除空目录或者文件

import org.testng.annotations.Test;

 

import java.io.*:

 

public class fileDirectory {

public static void main(Stringl] args){

 

}

@Test

//删除文件

public void fileDelete(){

StringfilePath="D:1\file1.txt;

File file=new File(filePath);

 

if(file.exists()){

if(file.delete()){

System.out.printin(filePath+“删除成功”);

}else {

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

};

}else{

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

}

//删除目录

@Test

public void fileDeleteD){

StringfilePath="D:1\file1.txt";

File file=new File(filePath);

 

if(file.exists()){

if(file.delete()){

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

}else {

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

 

};

}else{

System.out.printin(“目录不存在”);

}

/判断目录是否存在,不存在就创建

 

@Test

public void fileDeleteD10{

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

File file=new File(dirPath);

 

if(file.exists()){

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

}else{

if(file.mkdirs0){

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

}else {

System.out.printin(“创建失败");

};

 

}

//lnputStream

//OutputStream

/Writer

//Reader

}

5ScannerPrintln

1、基本键盘输入

 

import org.testng.annotations.Test;

 

import java.util.Scanner

public class scanPrintTest{

public static void main(String[] args){

//创建Scanner对象,接受从控制台输入

Scanner input=new Scanner(Systemin);

//接受String类型

String str=input.next();

//输出结果

System.out.printin(str);

 

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

2 常见键盘输入类型

importjava.util.Scanner;

 

public class scanTest {

 

public static void main(String args){

Scanner input=new Scanner(Systemin);

//double类型的数据

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

doubled=input.nextDouble();

System.out.println(d);

//int类型的数据

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

inti=input.nextint;

System.out.println(i);

//字符串类型的数据

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

Strings=input.next();

System.out.println(s);

}

6InputStream

常见的子类

FilelnputStream:文件输入流

BufferedStream:缓冲字节输入流

ObiectedStream:对象字节输入流

 

import org.testng.annotations.Test;

importjava.io.FilelnputStream;

import java.io.FileNotFoundException

import java.io.lOException

 

public class filelnputStream{

public static void main(Stringl]args){

 

}

@Test

public void fileln1(){

String filePath="D:1\testfile.txt";

int readData;

FilelnputStream filelnputStream=null;

try{

 

//创建FilelnputStream对象,用于读取文件

filelnputStream=new FilelnputStream(filePath);

 

while((readData=filelnputStream.read()!=-1){

System.out.printn((char)readData);

};

}catch (IOException e){

e.printStackTrace0;

}finally {

//关闭文件流

try {

filelnputStream.close();

}catch (IOException e) {

throw new RuntimeException(e);

}

}

 

}

@Test

public void fileln20{

String filePath="D:1\testfile.txt;

int readData;

int readlength=0;

//字节数组

byte[]buf=new byte[8];

FilelnputStream filelnputStream=null

try{

//创建FilelnputStream对象,用于读取文件

filelnputStream=new FilelnputStream(filePath);

 

while((readlength=filelnputStream.read(buf))!=-1){

System.out.println(new String(buf,readlength));

};

 

}catch(OException e){

e.printStackTrace();

}finally {

//关闭文件流

try {

filelnputStream.close()

}catch(IOExceptione){

throw new RuntimeException(e)

}

7OutputStream

1FileOutputStream

例子:使用FileOutputStream在文件夹中写入“hello world”,如果文件不存在,会创

建文件

import org.testng.annotationsTest

 

import java.io.FileNotFoundException

import java.io.FileOutputStream;

import java.io.OException

 

public class outputStream{

 

public static void main(Stringl] args){

 

}

//使用FileOutputStream将数据写到文件中

 

//如果文件不存在,则创建文件

@Test

public void writeFile0{

 

String filePath="d:\\fileout.txt";

 

//创建对象

FileOutputStream fileOutputStream=null;

import org.testng.annotationsTest

 

import java.io.FileNotFoundException

import java.io.FileOutputStream;

import java.io.OException

 

public class outputStream{

 

public static void main(Stringl] args){

 

}

//使用FileOutputStream将数据写到文件中

 

//如果文件不存在,则创建文件

@Test

public void writeFile0{

 

String filePath="d:\\fileout.txt";

 

//创建对象

FileOutputStream fileOutputStream=null;

try {

fileOutputStream=newFileOutputStream(filePath);

 

//1、写入一个字节

//fileOutputStream.write(H);

 

//2、写入一个字符串

//String str="hello socket";

//strgetBytes)将字符串转换成字节数组

//fileOutputStream.write(str.getBytes0);

//3、写入字符串

String str ="hello Ist";

fileOutputStream.write(str.getBytes()0strlength0);

 

} catch(IOExceptione){

e.printStackTrace();

}finally { try {

fileOutputStream.close();

} catch(IOException e){

//3、写入字符串

String str ="hello Ist";

fileOutputStream.write(str.getBytes()0strlength0);

 

} catch(IOExceptione){

e.printStackTrace();

}finally { try {

fileOutputStream.close();

} catch(IOException e){

Throw new runtimeException(e);

}

}

}

}

8、文件复制

importjava.io.File

import java.io.FilelnputStream;

import java.io.FileOutputStream;

 

public class copyPic{

//file读和写实现复制文件

public static void main(String]args)throws Exception{

//创建file对象

File f=new File("d:\\testjpg);

//判断文件是否存在

if(f.exists0){

System.out.println("test.jpg存在,可以复制");

}else{

f.createNewFile0;

System.out.println("testjpg不存在,新建成功,可以复制”);

}

//创建FilelnputStream对象

FilelnputStream inp=new FilelnputStream(f);

//创建FileOutputStream 对象

//判断demo目录是否存在

File f1=newFile("d:\demo");

if(f1.isDirectory(0)){

FileOutputStream out=new FileOutputStream("e:1demoll"+fgetName());

byte bytes[=new byte[1024];

inttemp=0;//边读边写

while((temp=inp.read(bytes))!=-1){/

out.write(bytestemp);//

}

//结束

inp.close0;

out.close();

System.out.printn("文件拷贝成功!");

}else{

//新建demo目录

f1.mkdir();

System.outprintln("demo目录不存在,已经新建成功,继续复制”);

FileOutputStream out=new FileOutputStream("d:\\demo\\"+f.getName());

byte bytes ]=new byte[1024];

inttemp=0;//边读边写

while((temp=inp.read(bytes))!=-1){//

out.write(bytestemp);/

}

//结束

inp.close();

out.close();

System.out.printn(文件拷贝成功!");

 

}

}

posted @   一点也不饿  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示