📄 dataoperationobject.java
字号:
package com.deskManager.File;
import java.io.*;
import java.util.*;
public class DataOperationObject {
public static String DataFileName = "data/proData/data.dat";
private String TempStrFile = "data/proData/datatemp.dat";
public ArrayList<Data> list() {
ArrayList<Data> re = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(DataFileName);
if (fis != null && fis.available() > 0) {
ObjectInputStream ois = new ObjectInputStream(fis);
re = new ArrayList<Data>();
Data d = null;
do {
try {
d = (Data) ois.readObject();
re.add(d);
// System.out.println(d);
} catch (Exception e) {
break;
}
} while (d != null);
ois.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return re;
}
public void SaveOrUpdate(Object o) {
Data d = (Data) o;
ArrayList<Data> temp = list();
if (temp == null) {
saveNew(d);
} else {
Data t;
int count = 0;
for (int i = 0; i < temp.size(); i++) {
t = temp.get(i);
if (t.id == d.id) {
count++;
}
}
if (count == 0) {
saveNew(d);
} else {
update(d);
}
}
}
protected void saveNew(Data d) {
if (!d.icon.equals("")) {
String Icon = "data/ProImage/" + d.id + ".jpg";
moveIcon(d.icon, Icon);
d.icon = "data/ProImage/" + d.id + ".jpg";
}
FileInputStream fis;
try {
fis = new FileInputStream(DataFileName);
FileOutputStream fos = new FileOutputStream(DataFileName, true);
ObjectOutputStream oos = null;
if (fis.available() <= 0) {
oos = new ObjectOutputStream(fos);
} else {
oos = new NoheadObjectStream(fos);
}
fis.close();
oos.writeObject(d);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void update(Data d) {
if (!d.icon.equals("data/ProImage/" + d.id + ".jpg")
&& !d.icon.equals("")) {
String Icon = "data/ProImage/" + d.id + ".jpg";
moveIcon(d.icon, Icon);
d.icon = ("data/ProImage/" + d.id + ".jpg");
}
FileOutputStream fos;
try {
fos = new FileOutputStream(TempStrFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
ArrayList<Data> temp = list();
Data t;
for (int i = 0; i < temp.size(); i++) {
t = temp.get(i);
if (t.id == d.id) {
oos.writeObject(d);
} else {
oos.writeObject(t);
}
}
oos.close();
fos.close();
changeFile();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void del(Object o) {
Data d = (Data) o;
FileOutputStream fos;
try {
fos = new FileOutputStream(TempStrFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
ArrayList<Data> temp = list();
for (int i = 0; i < temp.size(); i++) {
Data t = temp.get(i);
if (t.id != d.id) {
oos.writeObject(t);
}
}
oos.close();
fos.close();
if (!d.icon.equals("")) {
File file = new File(d.icon);
if (file.isFile()) {
file.delete();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
changeFile();
}
protected void changeFile() {
FileInputStream fis;
try {
fis = new FileInputStream(TempStrFile);
ObjectInputStream ois = null;
FileOutputStream fos = new FileOutputStream(DataFileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
if (fis != null) {
ois = new ObjectInputStream(fis);
Data d = null;
do {
try {
d = (Data) ois.readObject();
oos.writeObject(d);
} catch (Exception e) {
break;
}
} while (d != null);
}
oos.close();
fos.close();
ois.close();
fis.close();
File file = new File(TempStrFile);
file.delete();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void taxis() {
ArrayList<Data> l = null;
l = list();
Data list[] = new Data[l.size()];
for (int i = 0; i < l.size(); i++) {
list[i] = l.get(i);
}
for (int i = 0; i < list.length - 1; i++) {
for (int m = 1; m < list.length; m++) {
if (list[i].name.compareToIgnoreCase(list[m].name) > 0) {
Data temp = null;
temp = list[i];
list[i] = list[m];
list[m] = temp;
}
}
}
FileOutputStream fos;
try {
fos = new FileOutputStream(DataFileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < list.length; i++) {
oos.writeObject(list[i]);
}
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void moveIcon(String old, String values) {
FileInputStream fis;
try {
fis = new FileInputStream(old);
File file = new File(values);
if (!file.isFile()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(values);
int n = 0;
do {
n = fis.read();
if (n != -1) {
fos.write(n);
}
} while (n != -1);
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("null")
public String[] groups() {
ArrayList<Data> list = list();
ArrayList<String> head = new ArrayList<String>();
if (list != null) {
for (int i = 0; i < list.size(); i++) {
Data d = list.get(i);
if (head != null) {
int count = 0;
for (String t : head) {
if (t.equals(d.group)) {
count++;
}
}
if (count == 0) {
head.add(d.group);
}
} else {
head.add(d.group);
}
}
}
String group[] = new String[head.size()];
int i = 0;
for (String h : head) {
group[i] = h;
i++;
}
return group;
}
public void changeGroup(ArrayList<Data> ar, String name) {
for (Data l : ar) {
l.group = name;
}
for (Data l : ar) {
update(l);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -