我迫切需要java中文件输入/输出的帮助

时间:2013-07-27 18:16:54

标签: java file

我需要有关输入文件(文件名为inp2.dat)并使用文件(文件名为out2.dat)在同一计划中输出的帮助。我是否在同一个班级中使用FileInputStreamFileOutputStream?请帮忙。它的输出文件未找到。

import java.io.*;
class MBF
{
static String fileinp="inp2.dat";
public void main() 
{
    boolean EOF=false;
    try
    {
        FileInputStream fr=new FileInputStream(fileinp);
        DataInputStream dr=new DataInputStream(fr);
        while(!EOF)
        {
            try
            {
    System.out.println("Enter no. of inputs:");
    int n=dr.readInt();
    int max=0;
    for(int a=1;a<=n;a++)
    {
        System.out.println("Enter:");
        int p=dr.readInt();
        String str=dr.readLine();
        max=max+1;
        if(str.charAt(1)==str.charAt(0))
        max=max+1;
        else
        max=max+2;
        for(int i=0;i<p-2;i++)
        {
            char f=str.charAt(i);
            char s=str.charAt(i+1);
            char t=str.charAt(i+2);
            if((f==s)&&(f==t))
            max=max+1;
            else
            if(((f==s)&&(f!=t))||((s==t)&&(f!=t))||((f==t)&&(t!=s)))
            max=max+2;
            else
            max=max+3;
        }
        max=0;
      }
    }
    catch(EOFException el)
    {
        System.out.println("end of file");
        EOF=true;
    }
    catch(IOException e)
    {
        System.err.println(e);
    }
    }
    }
    catch(FileNotFoundException e)
    {
        System.out.println("File not found");
    }
   }
}

1 个答案:

答案 0 :(得分:0)

总有一些问题。我对评论进行了一些修改:

class MBF
{    
    static String fileinp = "C:\\inp2.dat";

    public static void main(String[] args) // main method signature was wrong 
    {
        // this line doesn't need to be outside of main, and you aren't using your fileinp string
        Scanner in = new Scanner(new File(fileinp)); 

        // added your writer
        BufferedWriter out = new BufferedWriter(new FileWriter("C:\\out2.dat"));
        System.out.println("Enter no. of inputs:");
        int n = in.readInt(); 
        //in.close(); I don't think you meant to do this here.
        int max=0;

        for(int a = 1; a <= n; a++)
        {
            System.out.println("Enter:");

            // these were originally dr.read... dr is not a variable in scope here. I think you meant in
            int p = in.readInt();
            String str = in.readLine();

            max=max+1;
            if(str.charAt(1)==str.charAt(0))
                max=max+1;
            else
                max=max+2;

            for(int i = 0; i < p - 2; i++)
            {
                char f = str.charAt(i);
                char s = str.charAt(i + 1);
                char t = str.charAt(i + 2);

                if ((f == s) && (f == t))
                    max=max+1;
                else if (((f == s) && (f != t)) || ((s == t) && (f != t)) || ((f == t) && (t != s)))
                    max=max+2;
                else
                    max=max+3;
            }

            out.write(max + "\n");

            max=0;
        }

        in.close();
        out.close();
    }
}

应该做你想做的事。