字节流和字符流
package demo;
import java.io.*;
/**
* <p> TODU </P>
*
* @author yeager
* @Version V1.1.1.0
* @date 2023/12/1 16:08
*/
public class OutputStreamDemo {
public static void main(String[] args)
{
FileWriterDemo("D:\\Hanyun\\code\\java-learning\\Day20231128\\out\\demo","OutputStreamDemo.txt");
System.out.println("----------------------------------------------------");
FileOutputStreamDemo("D:\\Hanyun\\code\\java-learning\\Day20231128\\out\\pic","OutputStreamDemo.jpg");
}
public static void FileWriterDemo(String path,String name)
{
FileWriter fileWriter = null;
File file = null;
try {
File filedir = new File(path);
boolean mkDir = filedir.mkdirs();
if(mkDir)
{
file = new File(filedir,name);
}
fileWriter = new FileWriter(file,false);
fileWriter.write("Hello this world");
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(fileWriter != null)
{
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void FileOutputStreamDemo(String path,String name)
{
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
File file = null;
try {
File filedir = new File(path);
boolean mkDir = filedir.mkdirs();
if(mkDir)
{
file = new File(filedir,name);
}
File source = new File("D:\\Hanyun\\code\\java-learning\\Day20231128\\out\\demo.jpg");
fileInputStream = new FileInputStream(source);
fileOutputStream = new FileOutputStream(file,false);
byte[] buffer = new byte[5];
int len = 0;
while ((len = fileInputStream.read(buffer)) != -1)
{
fileOutputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(fileOutputStream != null)
{
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fileInputStream != null)
{
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}