java实现输入输出流代码分享
作者:hebedich 发布时间:2023-11-18 01:03:45
标签:java,输入输出流
1,编写一个程序,读取文件test.txt的内容并在控制台输出。如果源文件不存在,则显示相应的错误信息。
package src;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File f = new File("src\\test.txt");//文件在src名为test.txt中
try {
FileReader fr = new FileReader(f);//将文件读取到内容中
int m;
while((m=fr.read())!=-){
System.out.print((char)(m));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2,编写一个程序实现如下功能,从当前目录下的文件fin.txt中读取80个字节(实际读到的字节数可能比80少)并将读来的字节写入当前目录下的文件fout.txt中
package src;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File f = new File("src\\fin.txt");//src下fin.txt文件
File f = new File("src\\fout.txt");//src下fout.txt文件
try {
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream(f);
byte[] temp = new byte[];//定义一个字节数组
fis.read(temp);//读到内存中
fos.write(temp);//写到文件
fis.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3,使用java的输入/输出流技术将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。
package src;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File f = new File("src\\fin.txt");//src下fin.txt文件
File f = new File("src\\fout.txt");//src下fout.txt文件
try {
FileReader fr = new FileReader(f);
FileWriter fw = new FileWriter(f);
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
String temp;
int i=;
while((temp=br.readLine())!=null){
bw.write(i+","+temp);
bw.newLine();//换行
i++;
}
bw.flush();//把缓冲区内容写到文件
br.close();
bw.close();
br.close();
bw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4,编写一个程序,接收从键盘输入的数据,并把从键盘输入的内容写到input.txt文件中,如果输入"quit",则程序结束。
package src;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
File f = new File("src\\input.txt");
try {
FileWriter fw = new FileWriter(f);
Scanner scanner = new Scanner(System.in);
String temp;
while(!((temp=scanner.nextLine()).equals("quit"))){
fw.write(temp);
}
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5,编写一个程序实现如下功能,文件fin.txt是无行结构(无换行符)的汉语文件,从fin中读取字符,写入文件fou.txt中,每40个字符一行(最后一行可能少于40个字)
package src;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File f=new File("src\\fin.txt");
File f=new File("src\\fout.txt");
try {
FileReader fr=new FileReader(f);
FileWriter fw=new FileWriter(f);
char temp[]=new char[];
int len;
while((len=fr.read(temp))!=-)
{
if(len==)
fw.write(new String(temp)+"\n");
else
fw.write(temp, , len);
}
fr.close();
fw.close();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
0
投稿
猜你喜欢
- C#连接本地.mdf文件:项目中右键点击,新增——数据——基于服务的数据库,项目下直接生成.mdf数据库文件,后台(数据库的写入用参数传递)
- Java中Static关键字的一些用法详解1. Static 修饰类属性,因为静态成员变量可以通过类名+属性名调用,非静态成员变量不能通过类
- 看一段程序String t = "a||b||c||d";String[] temp = t.split("\
- 本文实例讲述了java识别一篇文章中某单词出现个数的方法。分享给大家供大家参考。具体如下:1. java代码:import java.io.
- 源码学习的好处不用多说,Mybatis源码量少、逻辑简单,将写个系列文章来学习。SqlSessionMybatis的使用入口位于org.ap
- TimeSpan 结构 表示一个时间间隔。命名空间:System 程序集:mscorlib(在 mscorlib.dll 中)说
- 首先:我们要建一个web项目接着: 我们先来导入struts的xml文件第一步:右击你的项目名,鼠标到MyEclipse会看到一个add s
- 这篇文章主要介绍了spring cloud Ribbon用法及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 使用ExecutorService来停止线程服务之前的文章中我们提到了ExecutorService可以使用shutdown和shutdow
- 本文实例讲述了Java矩阵连乘问题(动态规划)算法。分享给大家供大家参考,具体如下:问题描述:给定n个矩阵:A1,A2,...,An,其中A
- 一、创建线程启动线程—start 方法通过覆写 run 方法创建⼀个线程对象,但线程对象被创建出来并不意味着线程就开始运
- Java 分割字符串java.lang.String 的 split() 方法, JDK 1.4 or laterpublic
- Java 8 lambda表达式引入详解及实例eclipse下载安装Help -> EclipseMarketplace ->
- git仓库直达List<String> strings = Lists.newArrayList("name=kk&q
- 如今,企业级应用程序的常见场景是同时支持HTTP和HTTPS两种协议,这篇文章考虑如何让Spring Boot应用程序同时支持HTTP和HT
- 主要从以下几方面来介绍一下@Scope注解@Scope注解是什么@Scope注解怎么使用@Scope注解的使用场景1,@Scope注解是什么
- 如果你发现在一个接口使用有如下定义方法: public String[] getParameters();那么你应该认
- 本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据比如json数据如下:{responseHeader:{status:0
- 由于毕业后工作没有对接到专业问题,导致四五年没有碰过Winform程序了。突然由于工作问题,为了方便自己,所以想自己写写小winform小软
- 1.char数组(字符数组)->字符串可以通过:使用String.copyValueOf(charArray)函数实现。举例:char