java源代码输出 java代码输出语句( 四 )


}
}
}
下面是输出部分:
Enter lines of text.
Enter ‘stop’ to quit.
This is line one.
This is line two.
Java makes working with strings easy.
Just create String objects.
stop
Here is your file:
This is line one.
This is line two.
Java makes working with strings easy.
Just create String objects.
java输入输出流与文件,求源代码!感谢大佬!你好,java的API中提供了用于对象输入输出文件的操作,实例代码如下:
定义单词类如下(注意:你定义的类要实现Serializable接口)
public class Words implements Serializable {
private int size;
private String[] words;
public Words(){};
public Words(String...strs){
this.words = strs;
this.size = strs.length;
}
@Override
public String toString() {
return "Words{" +
"size=" + size +
", words=" + Arrays.toString(words) +
'}';
}
}
2. 对象输入输出api测试类
public class ObjIOTest {
public static void main(String[] args) {
String path = "d:/myIOTest.txt";
ObjIOTest objIOTest = new ObjIOTest();
Words words = new Words("hello", "my", "dear", "friend");
try {
objIOTest.writeObject(path,words);
Words wordsFromFile = (Words)objIOTest.readObject(path);
System.out.println(wordsFromFile.toString());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
//java serialize a object to file
public void writeObject(String path,Object map) throws IOException{
File f=new File(path);
FileOutputStream out=new FileOutputStream(f);
ObjectOutputStream objwrite=new ObjectOutputStream(out);
objwrite.writeObject(map);
objwrite.flush();
objwrite.close();
}
// read the object from the file
public Object readObject(String path) throws IOException, ClassNotFoundException{
FileInputStream in=new FileInputStream(path);
ObjectInputStream objread=new ObjectInputStream(in);
Object map=objread.readObject();
objread.close();
return map;
}
}
把两段代码拷贝到一个包下即可运行了,希望您的问题得到解答
java编写一个程序输出JRE是什么??JRE就是java的运行环境,java相比于其他编程语言 , 有一个跨平台的优点,在这里发挥作用的就是jre,它为用Java写的程序提供了一个虚拟的环境(采用了JVM技术),任何java程序只在这个环境内运行,而与操作系统无关 。
编写java源程序
java源文件:指存储java源码的文件 。
先来看看如下代码://MyTest被public修饰 , 故存储该java源码的文件名为MyTest
public class MyTest {
public static void main(String[] args){
System.out.println("Test Java execute process.");
}
}
//由于MyTest被public修饰了,故Class A不能用public修饰
class A{}
//由于MyTest被public修饰了,故Class B不能用public修饰
class B{}
【java源代码输出 java代码输出语句】关于java源代码输出和java代码输出语句的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

推荐阅读