@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());
//从0开始只写入三个字节
//fos.write(str.getBytes(),0,3);
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);
// 有对多长,就读多少字节。(int) file.length()
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;
//每次读取1024个字节 -1表示文件末尾
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);
//从0开始 写入3个字符
//fos.write(str.toCharArray(),0,3);
//关闭流
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类型数组 一次读1024
char[] bt1 = new char[1024];
//默认为0 结束为-1
int readLen = 0;
//声明字符串
String str = "";
//开始循环 一次读取1024
while ((readLen = fis.read(bt1)) != -1) {
//字符串赋值 从0开始 到-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 = "";
//末尾是返回null
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;
//每次读取1024个字节 -1表示文件末尾
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); //实现序列化 Serializable
objectOutputStream.writeBoolean(true);//实现序列化 Serializable
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"));
//获取所有的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"));
//获取所有的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 +
'}';
}
}
08-26
09-08
183