java线IO与C++ IO?

java线IO与C++ IO?,java,c++,file-io,Java,C++,File Io,请注意,这并不比讨论好 我是一个C++程序员,它让我觉得不知道如何做太多的java文件IO .< /P>是愚蠢的。 我需要在一个文件中存储许多不同的数据类型,以便以后读取。其中包括整数和长度可变的字符串 C++中,我只能使用: //wont actually know the value of this string mystr("randomvalue"); //the answer to the Ultimate Question of Life, the Universe, and Eve

请注意,这并不比讨论好

<>我是一个C++程序员,它让我觉得不知道如何做太多的java文件IO .< /P>是愚蠢的。 我需要在一个文件中存储许多不同的数据类型,以便以后读取。其中包括整数和长度可变的字符串

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;
非常感谢

您需要了解java文件IO。

您需要了解java文件IO。

在java中,您使用或 对于原始二进制I/O。您可以在这些I/O类型之上组合其他I/O类型,以添加功能。例如,您可以使用使任意输入流成为缓冲流。在读取或写入二进制数据时,通常可以方便地在原始输入和输出流之上创建or,这样您就可以序列化任何基元类型,而无需首先将它们转换为字节表示形式。除了原语之外,序列化对象时,使用and。对于文本I/O,将原始字节流转换为基于行的字符串输入,您还可以使用BufferedReader和FileReader,而 同样,将格式化文本写入原始字节流也很容易。Java中的I/O远不止这些,但这些应该让您开始学习

例如:

void writeExample() throws IOException {
   File f = new File("foo.txt");
   PrintStream out = new PrintStream(
                         new BufferedOutputStream(
                             new FileOutputStream(f)));
   out.println("randomvalue");
   out.println(42);
   out.close();
}

void readExample() throws IOException {
   File f = new File("foo.txt");
   BufferedReader reader = new BufferedReader(new FileReader(f));
   String firstline = reader.readLine();
   String secondline = reader.readLine();
   int answer;
   try {
     answer = Integer.parseInt(secondline);
   } catch(NumberFormatException not_really_an_int) {
     // ...
   }
   // ...

}
在Java中,使用or 对于原始二进制I/O。您可以在这些I/O类型之上组合其他I/O类型,以添加功能。例如,您可以使用使任意输入流成为缓冲流。在读取或写入二进制数据时,通常可以方便地在原始输入和输出流之上创建or,这样您就可以序列化任何基元类型,而无需首先将它们转换为字节表示形式。除了原语之外,序列化对象时,使用and。对于文本I/O,将原始字节流转换为基于行的字符串输入,您还可以使用BufferedReader和FileReader,而 同样,将格式化文本写入原始字节流也很容易。Java中的I/O远不止这些,但这些应该让您开始学习

例如:

void writeExample() throws IOException {
   File f = new File("foo.txt");
   PrintStream out = new PrintStream(
                         new BufferedOutputStream(
                             new FileOutputStream(f)));
   out.println("randomvalue");
   out.println(42);
   out.close();
}

void readExample() throws IOException {
   File f = new File("foo.txt");
   BufferedReader reader = new BufferedReader(new FileReader(f));
   String firstline = reader.readLine();
   String secondline = reader.readLine();
   int answer;
   try {
     answer = Integer.parseInt(secondline);
   } catch(NumberFormatException not_really_an_int) {
     // ...
   }
   // ...

}

在C++字符串读取字符串中;是函数的声明,它返回字符串而不是字符串的定义。必须删除括号或在字符串之前添加类关键字。可以帮助处理类似>>的内容。这是一个很好的少一点痛苦,然后阅读每一行和手动转换-因为字符串Read字符串,您的C++示例被破坏;不是在做你想的事。你也知道如果使用随机值而不是随机值,会发生什么?C++中的字符串读写字符串;是函数的声明,它返回字符串而不是字符串的定义。必须删除括号或在字符串之前添加类关键字。可以帮助处理类似>>的内容。这是一个很好的少一点痛苦,然后阅读每一行和手动转换-因为字符串Read字符串,您的C++示例被破坏;不是在做你想的事。另外,您知道如果使用随机值而不是随机值会发生什么吗?+1尽管我相信>>的惯用Java将是a。+1尽管我相信>>的惯用Java将是a。