网站首页 > 文章中心 > 其它

java的输入输出类代码

作者:小编 更新时间:2023-09-16 14:46:19 浏览量:439人看过

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) ◆

'}';

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编写的有输入输出流源代码

/**

* 读写指定文件或者读写指定某一个文件夹下的全部文件(不包括文件夹)

* @throws Exception

*/

public static void moveFile() throws Exception {

Scanner scan = new Scanner(System.in);

System.out.println("请输入源路径:");//输入源文件地址,可以是文件夹,可以是具体某个文件

String uDisk = scan.nextLine();

File file = new File(uDisk);//获得读取文件

if ((file.exists())) {//当文件存在

System.out.println("请输入目标路径:");//文件复制目标路径

String targetFolder = scan.nextLine();

File target = new File(targetFolder);//获得写入文件

if (!target.exists()) {

if (!target.mkdir()) {

throw new Exception("创建目标目录失败");

} else if (!target.isDirectory()) {

throw new Exception("与目标目录同名的文件已经存在");

File[] temp = null;

if(file.listFiles()==null || file.listFiles().length=0){

temp=new File[]{file};//输入的源路径指定文件,将文件添加到文件数组中

}else{

temp = file.listFiles();//如果输入的源路径是文件夹,则获取文件夹下文件的个数

if ((temp != null) (temp.length 0)) {//文件数组temp有值

int i = 0;

for (int length = temp.length; i length; i◆◆) {//循环文件数组

if (!temp[i].isDirectory()) {

String fileName = temp[i].getName();

File t = new File(targetFolder ◆ File.separator

◆ fileName);//创建输出文件

if (!t.createNewFile()) {

throw new Exception("创建输出文件失败");

FileOutputStream out = new FileOutputStream(t);//创建文件输出流

FileInputStream in = new FileInputStream(temp[i]);//创建文件输入流

while (in.read(buffer) 0) {//循环输入流,直到输入流无数据

out.write(buffer);//写入文件

调用例子:

public static void main(String[] args) throws Exception {

moveFile();

Java的常用输入输出语句?

常用的输入语句是:

输入字符串:new Scanner(System.in).next();

输入整数:new Scanner(System.in).nextInt();

输入小数:new Scanner(System.in).nextDouble();

常用的输出语句:

换行输出: System.out.println(变量或字符串);

非换行输出: System.out.print(变量或字符串);

换行输出错误提示(默认是红字):System.err.println(变量或字符串);

不换行输出错误提示(默认是红字): System.err.print(变量或字符串));

以上就是土嘎嘎小编为大家整理的java的输入输出类代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章