当前位置: 代码迷 >> Java相关 >> java IO stream的问题
  详细解决方案

java IO stream的问题

热度:708   发布时间:2012-05-29 11:27:06.0
java IO stream的问题
1、从键盘上输入一些字符,可以有回车,以”#”结束输入;将输入的这些字符写到文本文件”a.txt”中     
2、读取”a.txt”中的内容,计算字符数和行数
求解
搜索更多相关的解决方案: java  文本文件  stream  

----------------解决方案--------------------------------------------------------
import java.io.*;
import java.util.*;
public class IO
{
    public void reader() throws IOException
    {
        FileReader fr=new FileReader("a.txt");
        char []buf=new char[1024];
        fr.read(buf);
        String str=new String(buf);
        
        System.out.println(str.trim());
    }
        
    public static void main(String[]arguments) throws IOException
    {
        InputStreamReader input =new InputStreamReader(System.in);
        BufferedReader reader=new BufferedReader(input);
        System.out.println("请输入字符:");
        String str=reader.readLine();
        StringTokenizer stream=new StringTokenizer(str,"#");
        String str1=stream.nextToken();
        FileWriter fw=new FileWriter("a.txt");
        fw.write(str1);
        fw.close();
        IO io=new IO();
        io.reader();
    }
}
----------------解决方案--------------------------------------------------------
我试了下,有点问题哦,就是当输入一些字符后想换行继续输,就按enter键,却直接输出了,题目里是当输入了#后才能结束啊
----------------解决方案--------------------------------------------------------
追加下问题:将这两个题放在一个类中

----------------解决方案--------------------------------------------------------
回复 2楼 ab1034982749
这个程序只能读去一行,如果你要读去多行,就自己试着改改
----------------解决方案--------------------------------------------------------
我已经做好了
----------------解决方案--------------------------------------------------------
读入不能用readlin()读入,要循环读入字符,判断读入“#”结束循环,在输出
----------------解决方案--------------------------------------------------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadChar {
    //得到字符数的方法;
    public static int getCharNumber(String FilePath) throws FileNotFoundException{
        File file=new File(FilePath);
        int number=(int)file.length();
        return number/2;
    }
    //得到行数的方法;
    public static int getLinesNumber(String FilePath) throws IOException{
        File file=new File(FilePath);
        int number=0;
        BufferedReader br=new BufferedReader(new FileReader(file));
        for(;;){
            if(br.readLine()==null) break;
            else number++;
        }
        br.close();
        return number;
        
    }
    //将字符写入文件;
    public static void writeFile(String FilePath,String content) throws IOException{
        File file=new File(FilePath);
        BufferedWriter bw=new BufferedWriter(new FileWriter(file));
        bw.write(content);
        bw.close();
    }
    //主方法;
    public static void main(String[] args) throws IOException {
        
        File file=new File("E:\\计算机文件\\JAVA\\hh.txt");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        char []charArray=new char[300];
        int k=0;
        for(;;){
            int i=br.read();
            char c=(char)i;
            if(c!='#') {
                charArray[k]=c;
                k++;
            }
            else break;
        }
        br.close();
        String readline=new String(charArray);
        System.out.println(readline.trim());
        
        writeFile("E:\\计算机文件\\JAVA\\hh.txt",readline.trim());
        
        int n=getLinesNumber("E:\\计算机文件\\JAVA\\hh.txt");
        System.out.println(n);
        
        int n1=getCharNumber("E:\\计算机文件\\JAVA\\hh.txt");
        System.out.println(n1);
    }
}
----------------解决方案--------------------------------------------------------
  相关解决方案