@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("字节流写入")
@RequestMapping(value = "/writFileTest", method = RequestMethod.POST, produces = "application/json")
public AjaxResult writFileTest() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\a.txt");
if (!file.exists()) {
boolean mkdirs = file.getParentFile().mkdirs();
boolean newFile = file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file, true);
String str = "SpoCsInPut";
fos.write(str.getBytes());
fos.close();
return AjaxResult.success();
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("字节流读取")
@RequestMapping(value = "/readFileTest", method = RequestMethod.POST, produces = "application/json")
public AjaxResult readFileTest() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\a.txt");
FileInputStream fis = new FileInputStream(file);
byte bt1[] = new byte[1024];
int len = fis.read(bt1);
String str = new String(bt1, 0, len);
fis.close();
return AjaxResult.success(str);
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("字节拷贝二进制图片")
@RequestMapping(value = "/FileCopy", method = RequestMethod.POST, produces = "application/json")
public AjaxResult FileCopy() {
String imgPath = "D:\\20220411File\\D.jpg";
String imgPathCopy = "D:\\20220411File\\A.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(imgPath);
fileOutputStream = new FileOutputStream(imgPathCopy);
byte[] bytes = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, readLen);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return AjaxResult.success("完成");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("字符流写入磁盘")
@RequestMapping(value = "/FileWriterOut", method = RequestMethod.POST, produces = "application/json")
public AjaxResult FileWriterOut() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\字符.txt");
if (!file.exists()) {
boolean mkdirs = file.getParentFile().mkdirs();
boolean newFile = file.createNewFile();
}
FileWriter fos = new FileWriter(file, true);
String str = "明天你好";
fos.write(str);
fos.close();
return AjaxResult.success("写入完成");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("字符流读取文件")
@RequestMapping(value = "/FileReaderIn", method = RequestMethod.POST, produces = "application/json")
public AjaxResult FileReaderIn() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\字符.txt");
FileReader fis = new FileReader(file);
char[] bt1 = new char[1024];
int readLen = 0;
String str = "";
while ((readLen = fis.read(bt1)) != -1) {
str = new String(bt1, 0, readLen);
}
fis.close();
return AjaxResult.success(str);
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Buffered缓冲流读取文件")
@RequestMapping(value = "/BufferedReaderCs", method = RequestMethod.POST, produces = "application/json")
public AjaxResult BufferedReaderCs() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\字符.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String str;
String reStr = "";
while ((str = bufferedReader.readLine()) != null) {
reStr = str;
}
bufferedReader.close();
return AjaxResult.success(reStr);
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Buffered缓冲流写入文件")
@RequestMapping(value = "/BufferedWriterCs", method = RequestMethod.POST, produces = "application/json")
public AjaxResult BufferedWriterCs() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\buffer.txt");
if (!file.exists()) {
boolean mkdirs = file.getParentFile().mkdirs();
boolean newFile = file.createNewFile();
}
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
bufferedWriter.write("测试缓冲流写入数据!");
bufferedWriter.newLine();
bufferedWriter.write("HelloDis!");
bufferedWriter.flush();
bufferedWriter.close();
return AjaxResult.success("写入成功");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("BufferedCopy文本文件")
@RequestMapping(value = "/BufferedCopy", method = RequestMethod.POST, produces = "application/json")
public AjaxResult BufferedCopy() throws DocumentException, IOException {
String imgPath = "D:\\20220411File\\buffer.TXT";
String imgPathCopy = "D:\\20220411File\\bufferA.TXT";
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader(imgPath));
bufferedWriter = new BufferedWriter(new FileWriter(imgPathCopy));
String read;
while ((read = bufferedReader.readLine()) != null) {
bufferedWriter.write(read);
bufferedWriter.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return AjaxResult.success("拷贝成功");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("BufferedCopy二进制文件")
@RequestMapping(value = "/BufferedStreamCopy", method = RequestMethod.POST, produces = "application/json")
public AjaxResult BufferedStreamCopy() throws DocumentException, IOException {
String imgPath = "D:\\20220411File\\D.jpg";
String imgPathCopy = "D:\\20220411File\\C.jpg";
BufferedInputStream fileInputStream = null;
BufferedOutputStream fileOutputStream = null;
try {
fileInputStream = new BufferedInputStream(new FileInputStream(imgPath));
fileOutputStream = new BufferedOutputStream(new FileOutputStream(imgPathCopy));
byte[] bytes = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, readLen);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return AjaxResult.success("拷贝成功");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Object保存文件为data,序列化")
@RequestMapping(value = "/ObjectOutputStreamCs", method = RequestMethod.POST, produces = "application/json")
public AjaxResult ObjectOutputStreamCs() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\data.data");
if (!file.exists()) {
boolean mkdirs = file.getParentFile().mkdirs();
boolean newFile = file.createNewFile();
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
objectOutputStream.writeInt(100);
objectOutputStream.writeBoolean(true);
objectOutputStream.writeChar('a');
objectOutputStream.writeDouble(93.5);
objectOutputStream.writeUTF("明天");
objectOutputStream.writeObject(new Dog("name", 10));
objectOutputStream.close();
return AjaxResult.success("写入成功");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Object读取文件为data,反序列化")
@RequestMapping(value = "/ObjectInputStreamCs", method = RequestMethod.POST, produces = "application/json")
public AjaxResult ObjectInputStreamCs() throws DocumentException, IOException, ClassNotFoundException {
File file = new File("D:\\20220411File\\data.data");
ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file));
System.out.println(stream.readInt());
System.out.println(stream.readBoolean());
System.out.println(stream.readChar());
System.out.println(stream.readDouble());
System.out.println(stream.readUTF());
Object dog1 = stream.readObject();
System.out.println(dog1);
Dog dog2 = (Dog) dog1;
System.out.println(dog2.getName());
stream.close();
return AjaxResult.success("控制台输出成功!");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Xml解析One")
@RequestMapping(value = "/CsXml", method = RequestMethod.POST, produces = "application/json")
@SuppressWarnings("unchecked")
public AjaxResult CsXml() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document read = reader.read(new File("D:\\20220411File\\CsXml.xml"));
String asXML = read.asXML();
Element rootElement = read.getRootElement();
List<String> list = new ArrayList<>();
for (Iterator<Element> it = rootElement.elementIterator("user"); it.hasNext();) {
Element user = it.next();
String id = user.attributeValue("id");
String name = user.elementText("name");
String age = user.elementText("age");
String address = user.elementText("address");
list.add(id+name+age+address);
}
return AjaxResult.success(list);
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Xml解析Two")
@RequestMapping(value = "/SAXReader", method = RequestMethod.POST, produces = "application/json")
@SuppressWarnings("unchecked")
public AjaxResult SAXReader() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document read = reader.read(new File("D:\\20220411File\\CsXml.xml"));
String asXML = read.asXML();
Element rootElement = read.getRootElement();
List<Element> user = rootElement.elements();
List<String> list = new ArrayList<>();
for (Element elemen:user) {
String id = elemen.attributeValue("id");
String name = elemen.elementText("name");
String age = elemen.elementText("age");
String address = elemen.elementText("address");
list.add(id+name+age+address);
}
return AjaxResult.success(list);
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Xml添加属性与值")
@RequestMapping(value = "/SaveSAXReader", method = RequestMethod.POST, produces = "application/json")
public AjaxResult SaveSAXReader() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document read = reader.read(new File("D:\\20220411File\\CsXml.xml"));
Element rootElement = read.getRootElement();
rootElement.addElement("user").addAttribute("id","p4");
rootElement.addElement("name").addText("小一");
rootElement.addElement("age").addText("20");
rootElement.addElement("address").addText("蜀国");
rootElement.addElement("user").addAttribute("id","p5");
rootElement.addElement("name").addText("小二");
rootElement.addElement("age").addText("21");
rootElement.addElement("address").addText("蜀国");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileWriter(new File("D:\\20220411File\\CsXml.xml")), format);
writer.write( read );
writer.close();
return AjaxResult.success("写入成功!");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Xml删除节点")
@RequestMapping(value = "/DelSAXReader", method = RequestMethod.POST, produces = "application/json")
public AjaxResult DelSAXReader() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("D:\\20220411File\\CsXml.xml"));
Element root = document.getRootElement();
Element node = (Element) document.selectSingleNode("//user[@id = 'p4']");
boolean remove = node.getParent().remove(node);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileWriter(new File("D:\\20220411File\\CsXml.xml")), format);
writer.write( document );
writer.close();
return AjaxResult.success("删除成功!");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Xml修改节点")
@RequestMapping(value = "/UpDateSAXReader", method = RequestMethod.POST, produces = "application/json")
public AjaxResult UpDateSAXReader() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("D:\\20220411File\\CsXml.xml"));
Element user = (Element) document.selectSingleNode("//user[@id = 'p3']");
user.element("name").addAttribute("type","dd");
user.element("name").setText("周瑜");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileWriter(new File("D:\\20220411File\\CsXml.xml")), format);
writer.write( document );
writer.close();
return AjaxResult.success("修改成功!");
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Properties文件读取")
@RequestMapping(value = "/PropertiesGet", method = RequestMethod.POST, produces = "application/json")
public AjaxResult PropertiesGet() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\mysql.properties");
Properties properties = new Properties();
properties.load(new FileReader(file));
properties.list(System.out);
String id = properties.getProperty("id");
return AjaxResult.success(id);
}
@Transactional(rollbackFor = RestClientException.class)
@ApiOperation("Properties文件写入修改")
@RequestMapping(value = "/SaveProperties", method = RequestMethod.POST, produces = "application/json")
public AjaxResult SaveProperties() throws DocumentException, IOException {
File file = new File("D:\\20220411File\\mysql.properties");
Properties properties = new Properties();
properties.setProperty("name","123");
properties.setProperty("age","1111");
properties.setProperty("address","中国");
properties.store(new FileWriter(file),"这是注释");
return AjaxResult.success("写入成功");
}
static class Dog implements Serializable {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}