有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要关于在同一程序中使用file(文件名为inp2.dat)输入和使用file(文件名为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) 个答案

  1. # 1 楼答案

    自始至终都有一些问题。我在评论中做了一些更改:

    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();
        }
    }
    

    你应该做你想做的