java输入输出流实例代码

java输入输出流实例代码1.编写一个程序,读取源代码文件的内容并在控制台输出。如果源文件不存在,则显示相应的错误信息。packagesrc;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;publicclasstest01{

大家好,又见面了,我是你们的朋友全栈君。

1.编写一个程序,读取源代码文件的内容并在控制台输出。如果源文件不存在,则显示相应的错误信息。

package src;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class test01 {

    public static void main(String[] args) {
        File f = new File("test01.java");//文件当前目录下,在eclipse下是该工程目录下。
        try {
            FileReader fr = new FileReader(f);//将文件读取到内容中
            int m;//int包含char的范围
            while((m=fr.read())!=-1){
                System.out.print((char)(m));//强制转为char
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

2.编写一个程序实现如下功能,从当前目录下的文件input.txt中读取80个字节(实际读到的字节数可能比80少)并将读来的字节写入当前目录下的文件output.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 test01 {

    public static void main(String[] args) {
        File f1 = new File("input.txt");
        File f2 = new File("output.txt");
        
        try {
            FileInputStream fis = new FileInputStream(f1);
            FileOutputStream fos = new FileOutputStream(f2);
            
            byte[] temp = new byte[80];//定义一个字节数组
            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();
        }
        System.out.println("运行结束");
    }
}

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 test01 {

    public static void main(String[] args) {
        File f1 = new File("input.txt");              
        File f2 = new File("output.txt");
        
        try {
            FileReader fr = new FileReader(f1);
            FileWriter fw = new FileWriter(f2);
            
            BufferedReader br = new BufferedReader(fr);
            BufferedWriter bw = new BufferedWriter(fw);
            
            String temp;
            int i=1;//行号
            while((temp=br.readLine())!=null){
                bw.write(i+","+temp);
                bw.newLine();//换行
                i++;
            }
            bw.flush();//把缓冲区内容写到文件,如果没有这条语句,输出文件为空。
//使用缓存型流时操作完成后必须加上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();
        }
        System.out.println("运行结束");
    }
}

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("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.编写一个程序实现如下功能,文件input.txt是无行结构(无换行符)的汉语文件,从input中读取字符,写入文件output.txt中,每10个字符一行(最后一行可能少于10个字)

/*
 * 注意设置input.txt为UTF-8格式,否则读取中文显示乱码
*/
package src;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class test01 {

    public static void main(String[] args) {
        File f1=new File("input.txt");
        File f2=new File("output.txt");
        try {
            FileReader fr=new FileReader(f1);
            FileWriter fw=new FileWriter(f2);
            
            char temp[]=new char[10];
            int len;
            while((len=fr.read(temp))!=-1)
            {
                if(len==10)
                  fw.write(new String(temp)+"\r\n");
                /* 
                 * windows下的文本文件换行符:\r\n linux/unix下的文本文件换行符:\r 
                 * Mac下的文本文件换行符:\n 
                 */ 
                else
                    fw.write(temp, 0, len);
            }
            fr.close();
            fw.close();
            
        } catch (FileNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        System.out.println("程序结束");
    }
}

6.逐行读取汉字文件,复制到另一个文件

package src;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;



public class test01 {
	public static void main(String[] args)  {
		File f = new File("input.txt");
                File outFile=new File("output.txt");
		InputStreamReader read = null;
		OutputStreamWriter write=null;

		try {
		read = new InputStreamReader (new FileInputStream(f));//注意事先设置好input.txt的编码格式为UTF-8,否则输出乱码
	        write=new OutputStreamWriter(new FileOutputStream(outFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		BufferedReader reader=new BufferedReader(read);

		String line;

		try {
			while ((line = reader.readLine()) != null) {

			System.out.println(line);
            write.write(line+"\r\n");
			}
			write.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/141435.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • _ctl0_ContentPlaceHolder1 或者 ctl00_ContentPlaceHolder1

    _ctl0_ContentPlaceHolder1 或者 ctl00_ContentPlaceHolder1当你使用masterpage的时候,页面内的服务端控件会自动加上 _ctl0_ContentPlaceHolder1或者ctl00_ContentPlaceHolder1,但什么时候是出现_ctl0_ContentPlaceHolder1,而又什么时候出现的是ctl00_ContentPlaceHolder1呢? 修改web.configxhtmlConformance mode=

    2022年7月13日
    13
  • 双管道(CreatePipe)与本地cmd.exe进程通信(附源代码及编译好的程序,免费下载)

    双管道(CreatePipe)与本地cmd.exe进程通信(附源代码及编译好的程序,免费下载)源代码:#include<stdio.h>#include<WINDOWS.H>#defineSEND_BUFF_SIZE1024//实现去除执行结果中的”命令\n”voidprint(char*cmdstr){ while(*((char*)cmdstr++)!=’\n’); printf(cmdstr);}intmai…

    2022年7月14日
    18
  • 微信小程序反编译教程_微信小程序防止反编译

    微信小程序反编译教程_微信小程序防止反编译最近写爬虫需要小程序反编译,过程总结下…..通过微信PC版,打开需要反编译的小程序,小程序将产生缓存文件(但他是加密的),通过”小程序解密包”工具将加密的缓存文件解密成为”XXX.wxapkg”文件然后通过wxappunpacker工具反编译下载地址:链接:https://pan.baidu.com/s/1nqg97PxkFTt1TnEWECikYQ提取码:bsu91.找到小程序加密文件微信PC版查看设置->文件管理->打开文件夹然后往上一层返回一步例如:………

    2025年5月28日
    3
  • 超简单的windows发包工具—小兵以太网测试仪

    超简单的windows发包工具—小兵以太网测试仪小兵以太网测试仪是一款windows平台下的发包工具。该软件小巧、易用、开源、免费。该软件的功能有:各种常见报文(包括arpipicmpudptcp等)的编辑与发送发包速率控制抓包对抓到的包进行修改编辑及发送将报文导出为tcpdump/ethereal/wireshark存档(pcap格式)从tcpdump/ethereal/wireshark存档导入报文发送巨帧(j

    2025年8月31日
    6
  • 推荐几款s60软件

    推荐几款s60软件用了一段时间E72(应该是E52),大概已经习惯了s60系统s60既不会出什么问题,也不会太出众,这份稳定,就是我所需要的介绍一下笔者常用的软件吧系统工具类360手机卫士虽然来电通也是很好的软件,但如

    2022年7月3日
    31
  • MSN contactlist grabber

    MSN contactlist grabber——msn_contact_grab.class.php——(转)/*Copyright 2007 Jonathan Street jonathan@torrentialwebdev.comThis program is free software; you can redistribute it and/or modify    it under the terms of

    2025年6月18日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号