java读,写文件以字节方式
【摘要】
还有一种以字符方式的,可以自行百度
import java.util.*;
import java.io.*;
public class arithmetic {
public static void...
还有一种以字符方式的,可以自行百度
import java.util.*;
import java.io.*;
public class arithmetic {
public static void main(String[] args) throws IOException {
File f=new File("./xuexi.txt");//创建文件对象
Scanner sc=new Scanner(System.in);
try {
f.createNewFile();//创建文件
}catch(IOException e) {
e.printStackTrace();
System.out.println("no one");
}
try {
OutputStream out=new FileOutputStream(f);//建立输出流
for(int i=0;i<3;i++) {
String s=sc.next(),a="\r\n";
byte[] arr=s.getBytes();//字节输出流
byte[] b=a.getBytes();//换行
out.write(arr);
out.write(b);
}
out.close();
}catch(IOException e) {
e.printStackTrace();
}
int count=0;
try {
FileInputStream reader=new FileInputStream(f);//创建一个读的对象输入流
byte[] d=new byte[1024];//字节读文件
while((count=reader.read(d))!=-1) { //当read读到最后是会返回-1
System.out.println(new String(d));
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
文章来源: blog.csdn.net,作者:肥学,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jiahuiandxuehui/article/details/117027912
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)