文本文件a.txt的内容以倒序的方式输出
2011-02-14 20:16
701 查看
package hello;
import java.io.*;
public class FileReverse {
File ifile;
FileReverse(String fname)throws IOException {
ifile=new File(fname);
}
public void reverse() {
int c;
FileInputStream fin;
FileOutputStream fout;
BufferedReader br;
BufferedWriter bw;
StringBuffer strbuf=new StringBuffer();
try{
fin = new FileInputStream(ifile);
br = new BufferedReader(new InputStreamReader(fin,"GB2312"));
while((c=br.read())!=-1) {
strbuf.append((char)c);
}
br.close();
fin.close();
fout = new FileOutputStream(ifile);
bw = new BufferedWriter(new OutputStreamWriter(fout,"GB2312"));
for(int j=strbuf.length()-1;j>=0;j--){
bw.write(strbuf.charAt(j));
System.out.println(String.valueOf(strbuf.charAt(j)));
}
bw.close();
fout.close();
}
catch(FileNotFoundException e){
System.err.println("io:"+e.getMessage());
}
catch (IOException e) {
System.err.println("io:"+e.getMessage());
}
}
public static void main(String []args) {
try{
FileReverse fc=new FileReverse("e:\\hello.txt");
fc.reverse();
}
catch(FileNotFoundException e){
System.err.println("io:"+e.getMessage());
}
catch (IOException e) {
System.err.println("io:"+e.getMessage());
}
}
}
import java.io.*;
public class FileReverse {
File ifile;
FileReverse(String fname)throws IOException {
ifile=new File(fname);
}
public void reverse() {
int c;
FileInputStream fin;
FileOutputStream fout;
BufferedReader br;
BufferedWriter bw;
StringBuffer strbuf=new StringBuffer();
try{
fin = new FileInputStream(ifile);
br = new BufferedReader(new InputStreamReader(fin,"GB2312"));
while((c=br.read())!=-1) {
strbuf.append((char)c);
}
br.close();
fin.close();
fout = new FileOutputStream(ifile);
bw = new BufferedWriter(new OutputStreamWriter(fout,"GB2312"));
for(int j=strbuf.length()-1;j>=0;j--){
bw.write(strbuf.charAt(j));
System.out.println(String.valueOf(strbuf.charAt(j)));
}
bw.close();
fout.close();
}
catch(FileNotFoundException e){
System.err.println("io:"+e.getMessage());
}
catch (IOException e) {
System.err.println("io:"+e.getMessage());
}
}
public static void main(String []args) {
try{
FileReverse fc=new FileReverse("e:\\hello.txt");
fc.reverse();
}
catch(FileNotFoundException e){
System.err.println("io:"+e.getMessage());
}
catch (IOException e) {
System.err.println("io:"+e.getMessage());
}
}
}
相关文章推荐
- 使用Java的输入输出流技术将Input.txt的内容逐行读出,每读出一行就顺序为其添加行号(从1开始,逐行递增),并写入到另一个文本文件Output.txt中
- "I am from shanghai"的多种倒序输出方式,“shanghai from am I”
- Dos命令窗口中内容输出到文本文件
- ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM
- 如何用C#将输出到文本文件中的内容换行?
- Python通过prettytable模块将输出内容如表格方式整齐
- 在非SQL客户端使用命令行方式定期连接SQL Server 服务器并模拟用户查询操作,同时输出信息内容
- php、js、java的正则替换文本方式制作模板内容输出
- 打印三角形、已三角形方式 输出数组中的内容
- 第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好
- 编写程序通过栈的方式将任意输入的字符串内容进行逆序输出。
- Magento使用正规的方式输出网页(使用phtml文件输出内容)
- 用递归法将输入的数按正序和倒序的方式输出
- DOS命令比较两个文本文件txt的内容差异
- 利用C#控制台输出txt文件的内容(一行行来的)
- 倒序输出单链表的内容
- Python输出内容的三种方式:print输出 python脚本执行 linux直接执行
- 从键盘输入若干个字符,逐个存到磁盘文件data.txt中,直到输入’\n’为止,并将建立的文本文件的内容在存入文件的同时,在屏幕上显示出来:
- double类型变量输出到文本文件(txt) 控制输出有效位数
- javascript输出内容,也就是直接在网页中输出内容的四种方式