十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
你好,java的API中提供了用于對(duì)象輸入輸出文件的操作,實(shí)例代碼如下:
公司主營(yíng)業(yè)務(wù):網(wǎng)站制作、成都做網(wǎng)站、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。成都創(chuàng)新互聯(lián)公司推出黃島免費(fèi)做網(wǎng)站回饋大家。
定義單詞類如下(注意:你定義的類要實(shí)現(xiàn)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. 對(duì)象輸入輸出api測(cè)試類
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;
}
}
把兩段代碼拷貝到一個(gè)包下即可運(yùn)行了,希望您的問(wèn)題得到解答
/**
* 讀寫指定文件或者讀寫指定某一個(gè)文件夾下的全部文件(不包括文件夾)
* @throws Exception
*/
public static void moveFile() throws Exception {
Scanner scan = new Scanner(System.in);
System.out.println("請(qǐng)輸入源路徑:");//輸入源文件地址,可以是文件夾,可以是具體某個(gè)文件
String uDisk = scan.nextLine();
File file = new File(uDisk);//獲得讀取文件
if ((file.exists())) {//當(dāng)文件存在
System.out.println("請(qǐng)輸入目標(biāo)路徑:");//文件復(fù)制目標(biāo)路徑
String targetFolder = scan.nextLine();
File target = new File(targetFolder);//獲得寫入文件
if (!target.exists()) {
if (!target.mkdir()) {
throw new Exception("創(chuàng)建目標(biāo)目錄失敗");
}
} else if (!target.isDirectory()) {
throw new Exception("與目標(biāo)目錄同名的文件已經(jīng)存在");
}
File[] temp = null;
if(file.listFiles()==null || file.listFiles().length=0){
temp=new File[]{file};//輸入的源路徑指定文件,將文件添加到文件數(shù)組中
}else{
temp = file.listFiles();//如果輸入的源路徑是文件夾,則獲取文件夾下文件的個(gè)數(shù)
}
if ((temp != null) (temp.length 0)) {//文件數(shù)組temp有值
int i = 0;
for (int length = temp.length; i length; i++) {//循環(huán)文件數(shù)組
if (!temp[i].isDirectory()) {
String fileName = temp[i].getName();
File t = new File(targetFolder + File.separator
+ fileName);//創(chuàng)建輸出文件
if (!t.createNewFile()) {
throw new Exception("創(chuàng)建輸出文件失敗");
}
FileOutputStream out = new FileOutputStream(t);//創(chuàng)建文件輸出流
FileInputStream in = new FileInputStream(temp[i]);//創(chuàng)建文件輸入流
byte[] buffer = new byte[256];
while (in.read(buffer) 0) {//循環(huán)輸入流,直到輸入流無(wú)數(shù)據(jù)
out.write(buffer);//寫入文件
}
}
}
}
}
}
調(diào)用例子:
public static void main(String[] args) throws Exception {
moveFile();
}
常用的輸入語(yǔ)句是:
輸入字符串:new Scanner(System.in).next();
輸入整數(shù):new Scanner(System.in).nextInt();
輸入小數(shù):new Scanner(System.in).nextDouble();
常用的輸出語(yǔ)句:
換行輸出: System.out.println(變量或字符串);
非換行輸出: System.out.print(變量或字符串);
換行輸出錯(cuò)誤提示(默認(rèn)是紅字):System.err.println(變量或字符串);
不換行輸出錯(cuò)誤提示(默認(rèn)是紅字): System.err.print(變量或字符串));