package iodamo;import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileEditer {
/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {     //首先实例化一个file对象
    File file=new File("F:/test.txt");
    if(file.exists()&&file.isFile()){
     System.out.println("文件已经存在,且为普通文件");
    }else{
     System.out.println("文件不存在,要创建一个新的文件");
    
    }
    //实例化一个BufferedReader读取键盘输入
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String list;
    
    //实例化一个DataOutputStream对象
    DataOutputStream dos=new DataOutputStream(new FileOutputStream(file));
    
    while ((list=br.readLine())!=null){
    
     if(list.toLowerCase().equals("结束")){
     break;
     }
    
       dos.writeBytes(list);
       
    }
    
    System.out.println("========已经完成写入===========");
    br.close();
    dos.close();
    
   BufferedReader brrd=new BufferedReader(new FileReader(file));
    while((list=brrd.readLine())!=null){
    
       System.out.println(list);
    }
        
  brrd.close();
System.out.println(list2);
  System.out.println("==========完成读取============"); }}

解决方案 »

  1.   

    文件写的时候就有问题
     dos.writeBytes(list);
    应该是
    dos.write(list.getBytes());writeBytes的api如下
    void java.io.DataOutputStream.writeBytes(String s) throws IOException
    Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. If no exception is thrown, the counter written is incremented by the length of s.
    即每个字符只写高位的8位,可见字母(在gbk、UTF-8下)最多是一个字节即8位的
    你可以试试下面这个,然后敲入字母看看是不是乱码
    list =new String(list.getBytes(),"UTF-16");
    dos.writeBytes(list);
      

  2.   


    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    public class ReadWriteTextFile {

    public void readwrite(String path,Scanner here){
    File file=new File(path);
    if(file.exists()&&file.isFile()){
    System.out.println("请选择操作:               1.读取            2.写入");
    FileReader fr=null;
    FileOutputStream fos=null;
    String type=null;
    try {
    type=here.nextLine();
    if("1".equals(type)){
    fr=new FileReader(file);
    char[] temp=new char[(int) file.length()];
    if(file.length()>0){
    while(fr.read(temp)!=-1){
    System.out.println(new String(temp));
    }
    }
    readwrite(path, here);
    }
    if("2".equals(type)){
    fos=new FileOutputStream(path);
    System.out.println("请输入要写入的内容:");
    String temp=here.nextLine();
    fos.write(temp.getBytes());
    readwrite(path, here);
    }
    else{
    System.out.println("请输入指定数字!");
    readwrite(path, here);
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally{
    try {
    if(null!=fr){
    fr.close();
    }
    if(null!=fos){
    fos.close();
    }

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }
    else{
    try {

    file.createNewFile();
    readwrite(path, here);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.print("无法创建文件,请另外输入一个路径:");
    String p=here.nextLine();
    readwrite(p, here);
    }
    }
    }
    public static void main(String[] args) {
    System.out.print("请输入路径:");
    Scanner s=new Scanner(System.in);
    String path=s.nextLine();
    new ReadWriteTextFile().readwrite(path,s);
    }
    }
    你从新写一个吧 可以参考我写的这个例子
      

  3.   

    读写的时候要用字符流FileWriter、FileReader,
    不要用字节流DataOutputStream、FileOutputStream,
    字符流FileWriter、FileReader可以读写中文