这两个问题用java代码谁会打啊 有没有人会 查了半天了 实在不知道怎么搞了 有没有人能帮我一下 需要代码和运行结果的截图 😭
详细一点?
重点是你的问题是啥,都没有问题
你可看看我这篇文章,里面不仅有代码,还有注释,相信你看过后就知道怎么做了
https://blog.csdn.net/m0_56981185/article/details/123716061
是这个不
注:(1)读我是读一行,你也可以看看其他函数
(2)是能成功的,你自己封装成函数就可以使用了
1、读文件:
File file = new File("****"); //文件地址的绝对路径
InputStreamReader in = new InputStreamReader (new FileInputStream(file),"UTF-8");
BufferedReader reader=new BufferedReader(in);
String line= reader.readLine();//读一行
2、写文件:
File file = new File("******"); //文件地址的绝对路径
Writer out1 = new FileWriter(file);
out1.write("hhh");//写入hhh
out1.close();
我回答你了
问题描述不清。。。
public class TestInputStreamReader {
public static void main(String[] args) {
copy("hello3.txt","hello5.txt");
}
/**
* 复制文件
* @param src
* @param target
*/
private static void copy(String src,String target){
File srcFile = new File(src);
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader reader = null;
File targetFile = new File(target);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter writer = null;
try{
// 解码
fis = new FileInputStream(srcFile);
isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
reader = new BufferedReader(isr);
// 编码
fos = new FileOutputStream(targetFile);
osw = new OutputStreamWriter(fos,StandardCharsets.UTF_8);
writer = new BufferedWriter(osw);
String str;
while ((str = reader.readLine()) != null){
writer.write(str);
writer.newLine();
writer.flush();
}
} catch (IOException e){
e.printStackTrace();
} finally {
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(isr != null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(osw != null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
什么叫IO流:
I:指的是InputStream,这是一个抽象类,最常用的子类是FileInputStream
O:值得是OutputStream,这也是一个抽象类,最常用的子类是OutputStream
流:由于在进行文件操作的时候大多数是用的byte数据,这些数据并不是一次性写入(读取),而是像水龙头那样慢慢的流(想象一下你接水的场景)
废话还是不多bb,先来一份简单的代码:
File file=new File("e:"+File.separator+"JavaLearn"+File.separator+"EleventhDemo"+File.separator+"1.txt");
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
System.out.println("父级目录创建成功");
}
if (!file.exists()){
file.createNewFile();
}
其中File.separator指的是当前系统的默认分隔符,这样写的原因是可以保证Java文件在Windows系统运行时和Linux系统运行时都不会出错
这段代码也很简单,主要就是创建一个文件。
字节和字符就差一个字,但是,熟悉Java数据基本类型的都知道。这俩货一个是byte,一个是String。那么我们在对文件进行输出操作的时候,就可以把需要输出的内容定义成String类型而不是byte字节型;
同样,Writer也是一个抽象类,当我们用于文件操作的时候,常用的子类就是FileWriter。
import java.io.*;
public class test {
public static void main(String[] args) throws Exception{
File file=new File("e:"+File.separator+"JavaLearn"+File.separator+"EleventhDemo"+File.separator+"1.txt");
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
System.out.println("父级目录创建成功");
}
if (!file.exists()){
file.createNewFile();
}
Writer out=new FileWriter(file);
String data="Hello World!";
out.write(data);
out.close();
}
}
https://blog.csdn.net/weixin_45708563/article/details/117321929
https://edu.csdn.net/skill/java/java-3ea06f6d049d4c079a88747ffd0cc51e
你要的答案,CSDN里的java教程里就有,绝对包你满意