请注意,这不是一个“好于”的讨论。Java线IO与C++ IO?
我是一名C++程序员,它让我感到非常愚蠢,不知道如何去做非常多的Java文件IO。
我需要在一个文件中存储一些不同的数据类型,以便稍后回读。这些包括整数和可变长度的字符串。
在C++中,我可以只使用:
//wont actually know the value of this
string mystr("randomvalue");
//the answer to the Ultimate Question of Life, the Universe, and Everything
int some_integer = 42;
//output stream
ofstream myout("foo.txt");
//write the values
myout << mystr << endl;
myout << some_integer << endl;
//read back
string read_string;
int read_integer;
//input stream
ifstream myin("foo.txt");
//read back values
//how to do accomplish something like this in Java?
myin >> read_string;
myin >> read_integer;
非常感谢!
在C++`串read_string();`是函数声明,它返回字符串和不string.You的认定中必须删除brackats或字符串之前添加`class`关键字。 – UmmaGumma 2011-02-10 07:13:17
[Scanner](http://download.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html)可以帮助使用类似于“>>”的东西。当阅读每一行并手动转换时,这会有点痛苦;-) – 2011-02-10 07:22:39
你的C++示例已经破坏,因为`string read_string();`没有做你明显想到的。你也知道如果你使用``random value'`而不是``randomvalue'``会发生什么? – 6502 2011-02-10 07:23:32