Java IO流
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java IO流相关的知识,希望对你有一定的参考价值。
IO流
文件
常用的文件操作
01:
package FileDemo01;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) {
}
@Test
public void create01()
{
String filePath = "D:\\\\JavaPathExercise\\\\new1.txt";
File file = new File(filePath);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("success to create");
}
@Test
public void create02()
{
File parentFile = new File("D:\\\\JavaPathExercise");
String fileName = "new2.txt";
//这里的file对象,在java程序中,只是一个对象
//只有执行了createNewFile方法,才会成为真正的,在硬盘创建该文件
File file = new File(parentFile,fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("success to create");
}
@Test
public void create03()
{
String parentPath = "D:\\\\";
String filePath = "JavaPathExercise\\\\new03.txt";
File file = new File(parentPath,filePath);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("success to create");
}
}
02:
package FileDemo01;
import org.junit.jupiter.api.Test;
import java.io.File;
public class FileInformation {
public static void main(String[] args) {
}
@Test
public void info()
{
File file = new File("D:\\\\JavaPathExercise\\\\new1.txt");
System.out.println("file name = "+file.getName());
System.out.println("文件的绝对路径 = "+file.getAbsolutePath());
System.out.println("文件父级目录 = "+file.getParent());
System.out.println("文件字节大小 = "+file.length());
System.out.println("文件是否存在 = "+file.exists());//T
System.out.println("是不是一个文件 = "+file.isFile());//T
System.out.println("是不是一个目录 = "+file.isDirectory());//F
}
}
03:
package FileDemo01;
import org.junit.jupiter.api.Test;
import java.io.File;
public class Director_Demo {
public static void main(String[] args) {
}
@Test
public void m1()
{
String filePath = "D:\\\\JavaPathExercise\\\\new1.txt";
File file = new File(filePath);
if (file.exists())
{
if (file.delete())
{
System.out.println("success to delete");
}
else
{
System.out.println("fail");
}
}
else
{
System.out.println("the file does not exist");
}
}
@Test
public void m2()
{
String filePath = "D:\\\\demo02";//删除的是目录
File file = new File(filePath);
if (file.exists())
{
if (file.delete())
{
System.out.println("success to delete");
}
else
{
System.out.println("fail");
}
}
else
{
System.out.println("the file does not exist");
}
}
@Test
public void m3()
{
String filePath = "D:\\\\JavaPathExercise02";//删除的是目录
File file = new File(filePath);
if (file.exists())
{
System.out.println("the file exists");
}
else
{
if (file.mkdirs())
{
System.out.println("success to create");
}
}
}
}
IO流原来及流的分类
IO流体系图-常用的类
FileInputStream介绍
01:
package FileDemo01;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStream {
public static void main(String[] args) {
}
@Test
public void readFile01()
{
String filePath = "D:\\\\JavaPathExercise\\\\new1.txt";
java.io.FileInputStream fileInputStream = null;
int readData = 0;
try {
fileInputStream = new java.io.FileInputStream(filePath);
while((readData = fileInputStream.read())!=-1)
{
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02()
{
String filePath = "D:\\\\JavaPathExercise\\\\new1.txt";
byte[] buf = new byte[8];
java.io.FileInputStream fileInputStream = null;
int readLen = 0;
try {
fileInputStream = new java.io.FileInputStream(filePath);
while((readLen = fileInputStream.read(buf))!=-1)
{
System.out.print(new String(buf,0,readLen));
}
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream介绍
01:
package FileDemo01;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStream01 {
public static void main(String[] args) {
}
@Test
public void writeFile()
{
String filePath = "D:\\\\JavaPathExercise\\\\new4.txt";
FileOutputStream fileOutputStream = null;
try {
// fileOutputStream = new FileOutputStream(filePath); //会覆盖原txt里面的内容
fileOutputStream = new FileOutputStream(filePath,true);//追加
//fileOutputStream.write('a');
String str = "hello,world";
//str.getBytes()将字符串转成字符数组
// fileOutputStream.write(str.getBytes());
fileOutputStream.write(str.getBytes(),0,str.length());
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
小练习
01:
package FileDemo01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
String srcFilePath = "D:\\\\JavaPathExercise\\\\luf.jpg";
String destFilePath = "D:\\\\JavaPathExercise02\\\\tyc.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new java.io.FileInputStream(srcFilePath);
fileOutputStream = new java.io.FileOutputStream(destFilePath);
byte[] buf = new byte[1024];
int readLen = 0;
while((readLen = fileInputStream.read(buf))!=-1)
{
//边读边写
fileOutputStream.write(buf,0,readLen);
}
System.out.println("ok!!!");
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}catch (IOException e)
{
e.printStackTrace();
}
}
}
}
01:
package FileReaderandFileWritter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) {
String filePath = "D:\\\\JavaPathExercise\\\\new5.txt";
FileReader fileReader = null;
int data = 0;
try {
fileReader = new FileReader(filePath);
while((data = fileReader.read())!=-1)
{
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (fileReader!=null)
{
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReader案例
01:
package FileReaderandFileWritter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static voi以上是关于Java IO流的主要内容,如果未能解决你的问题,请参考以下文章
JAVA IO流相关代码(Serializable接口,管道流PipedInputStream类,RandomAccessFile类相关代码)
JAVA IO流相关代码(Serializable接口,管道流PipedInputStream类,RandomAccessFile类相关代码)