PrintIO 【013】

本文提供了三个Java程序示例,展示了如何使用PrintStream和PrintWriter进行文件输出,从文件读取内容并输出到控制台,以及如何接收用户输入并将其记录到文件中。这些示例涵盖了基本的文件I/O操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

*****1.**********************************************************************
[code]
package Testioprint;
import java.io.* ;
public class Test1 {
public static void main(String[] args) {
PrintStream ps = null ;
try {
FileOutputStream fos = new FileOutputStream("d:\\java\\test7.txt") ;
ps = new PrintStream(fos) ;
} catch(FileNotFoundException e) {
e.printStackTrace();
}
if(ps != null) {
System.setOut(ps) ;
}
int i=0 ;
for(char c=0; c<= 60000; c++) {
System.out.print(c +" ") ;
if(i ++>= 100) {
System.out.println() ; //换行
i = 0 ; //将i复原
}
}

}

}

[/code]
*******2**************************************************************
[code]
package Testioprint;
import java.io.* ;
public class test2 {
public static void main(String[] args) {
String filename = args[0] ; //命令行参数
if(filename != null) {
list(filename, System.out) ;
}
}
public static void list(String f, PrintStream fs) {
try {
BufferedReader br = new BufferedReader(new FileReader(f)) ;
String s = null ;
while((s=br.readLine())!= null) {
fs.println(s) ;
}
br.close() ;
} catch (IOException e) {
fs.println("read file error") ;
}
}

}
[/code]
*****3*******************************************************************
[code]
package Testioprint;
import java.io.*;
import java.util.* ;
public class test3 {
public static void main(String []args) {
int year,month,day ;
year = Calendar.getInstance().get(Calendar.YEAR) ;
month = Calendar.getInstance().get(Calendar.MONTH) ;
day = Calendar.getInstance().get(Calendar.DATE) ;
String s = null ;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
try {
PrintWriter pw = new PrintWriter(new FileWriter("d:/java/tests.txt",true)) ;
while ((s=br.readLine()) != null) {
if(s.equalsIgnoreCase("exit"))
break ;
System.out.println(s.toUpperCase()) ; //输出转成大写的字符
pw.println(s.toUpperCase()) ; //打印 String s
pw.println("------------") ;
pw.flush();
}
pw.println("==新添加日期是:"+ year+"年-"+ month +"-月-"+ day +"-日"+ "==" ) ;
pw.println() ;
pw.flush();
pw.close();
} catch(IOException e) {
e.printStackTrace();
}
}

}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值