如何在 Java 中创建输入文件(即 .in 文件)?
How do I create a input file(i.e .in file) in Java?
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
public class Checkfile
{
public static void main(String args[]) throws IOException
{
OutputStream os = new FileOutputStream("A.in");
Random rand = new Random();
int t = rand.nextInt(100);
os.write(t);
//System.out.println(t);
while (t-- > 0)
{
int y=rand.nextInt(3000);
os.write(y);
}
}
}
我得到的是不寻常的数字,而不是 Integer.I 认为它们是十六进制数字。我在这里尝试为竞争性编程问题编写测试用例生成器。我基本上想在 Java-
中编写以下 C++ 代码
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
freopen("n1.in","w",stdout);
srand(5);
int t=rand()%99+1;
assert(t<=100 && t>=1);
cout<<t<<endl;
while(t--)
{
int n=rand()%99+3;
assert(n<=100 && n>=3);
cout<<n<<endl;
}
}
提前致谢!
经过大量搜索,我发现这里唯一的错误是我们不能将整数或字符串以外的任何其他数据类型直接写入文件。所有其他数据类型以二进制格式进入文件。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
public class Checkfile
{
public static void main(String args[]) throws IOException
{
OutputStream os = new FileOutputStream("A.in");
Random rand = new Random();
int t = rand.nextInt(100);
os.write(t);
//System.out.println(t);
while (t-- > 0)
{
int y=rand.nextInt(3000);
os.write(y);
}
}
}
我得到的是不寻常的数字,而不是 Integer.I 认为它们是十六进制数字。我在这里尝试为竞争性编程问题编写测试用例生成器。我基本上想在 Java-
中编写以下 C++ 代码#include <bits/stdc++.h>
using namespace std;
int main(void)
{
freopen("n1.in","w",stdout);
srand(5);
int t=rand()%99+1;
assert(t<=100 && t>=1);
cout<<t<<endl;
while(t--)
{
int n=rand()%99+3;
assert(n<=100 && n>=3);
cout<<n<<endl;
}
}
提前致谢!
经过大量搜索,我发现这里唯一的错误是我们不能将整数或字符串以外的任何其他数据类型直接写入文件。所有其他数据类型以二进制格式进入文件。