资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java流的代码 java 流flush

java输入输出流与文件,求源代码!感谢大佬!

你好,java的API中提供了用于对象输入输出文件的操作,实例代码如下:

创新互联是一家专业提供垣曲企业网站建设,专注与成都做网站、网站设计、HTML5、小程序制作等业务。10年已为垣曲众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

定义单词类如下(注意:你定义的类要实现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的流操作,在公司的项目中长用到么?求具体代码

io。就是操作文件嘛。在公司经常有用到的。

现在是讲例子1:

//创建一个新文件

importjava.io.*;

classhello{

publicstaticvoidmain(String[] args) {

File f=newFile("D:\\hello.txt");

try{

f.createNewFile();

}catch(Exception e) {

e.printStackTrace();

}

}

}

//程序运行之后,在d盘下会有一个名字为hello.txt的文件。

例子2:

//File类的两个常量

importjava.io.*;

classhello{

publicstaticvoidmain(String[] args) {

System.out.println(File.separator);

System.out.println(File.pathSeparator);

}

}

运行结果:\

;

现在用常量来改动写上面的代码:

importjava.io.*;

classhello{

publicstaticvoidmain(String[] args) {

String fileName="D:"+File.separator+"hello.txt";

File f=newFile(fileName);

try{

f.createNewFile();

}catch(Exception e) {

e.printStackTrace();

}

}

}

用这些,建议使用常量,因为代码写的少了。

案例3:

//删除一个文件

importjava.io.*;

classhello{

publicstaticvoidmain(String[] args) {

String fileName="D:"+File.separator+"hello.txt";

File f=newFile(fileName);

if(f.exists()){

f.delete();

}else{

System.out.println("文件不存在");

}

}

}

案例4:创建一个文件夹

importjava.io.*;

classhello{

publicstaticvoidmain(String[] args) {

String fileName="D:"+File.separator+"hello";

File f=newFile(fileName);

f.mkdir();

}

}

案例5:列出指定目录的全部文件(包括隐藏文件):

importjava.io.*;

classhello{

publicstaticvoidmain(String[] args) {

String fileName="D:"+File.separator;

File f=newFile(fileName);

String[] str=f.list();

for(inti =0; i str.length; i++) {

System.out.println(str[i]);

}

}

}

大概的简单的东西,就这些了吧。你自己好好看看吧,欢迎追问。

下面关于java的输入流的代码,为什么只能进行一次输入呀?

你需要重复输入整数类型的时候你用while(scanner.hasNextInt()){ ....}当你输入的精度比整数类型高时,如12.34,hasNextInt()的值是false。就不可以进行多次输入。

同时,你如果想重复输入确定的个数,就用基本的for语句。还有什么不清楚可以追问。

java 文件流 下面的代码中 read是代表了什麼阿 。长度不是用字节数组表示的吗

多看看api,有助于提高以后你自己解决问题的能力。

public int read(byte[] b) throws IOException

从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。

返回:

读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回 -1。

read 返回的就是上面所说的返回。就是已经被读取到的字节长度,若到流末尾则返回-1。


名称栏目:java流的代码 java 流flush
本文链接:http://cdkjz.cn/article/ddjggdh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220