【问题标题】:how to convert freopen of c++ in I/O stream of java如何在java的I/O流中转换C++的freopen
【发布时间】:2019-07-15 12:05:52
【问题描述】:

由于我在 java 中编码,我必须读取文件并在 java 上写入

我有 C++ 代码

freopen("addin.txt","r",stdin); freopen("addin.txt","w",stdout);

我必须在java中转换这段代码我怎么能?

i have tried this in java.

int frw(){
File f1=new File("addin.txt","r");
File f2=new File("addout.txt","w");
FileInputStream fi=new FileInputStream(f1);
FileOutputStream fo=new FileOutputStream(f2);
a=s.nextInt(fi.read());    
b=s.nextInt(fi.read());   
fo.write(b);    
}

this is code in c++ which i to convert into java

int main()
{
freopen("addin.txt","r",stdin);
freopen("addout.txt","w",stdout); 
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}

【问题讨论】:

    标签: java freopen


    【解决方案1】:
    public class  Main {   
      static private  final String INPUT =  "input.txt" ;   
      static private  final String OUTPUT =  "output.txt" ;   
    
      public static void  main (String args []) {    
          // open I / O files  
          FileInputStream instream = null;  
          PrintStream outstream = null;  
    
          try  {  
              instream =  new  FileInputStream (INPUT);  
              outstream =  new  PrintStream ( new  FileOutputStream (OUTPUT));  
              System.setIn (instream);  
              System.setOut (outstream);  
          }  catch  (Exception e) {  
              System.err.println ( "Error Occurred." );  
          }  
    
          Scanner in =  new  Scanner (System.in); 
          int a =  in.nextInt ();  
          int b =  in.nextInt ();
          System.out.println (a+b);
    
      }  
    
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 2023-03-08
      • 2015-05-24
      • 2012-12-20
      • 2018-09-10
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多