java代码 读取文件(如sql文件等)

java代码 读取文件(如sql文件等)

package com.unicom.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileOperation {

	/**
	 * 创建文件
	 * @param fileName
	 * @return
	 */
	public static boolean createFile(File file) {
		boolean flag = false;
		try {
			if (!file.exists()) {
				file.createNewFile();
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 读取文件内容
	 * @param filePathAndName String 如 c:\\1.txt 绝对路径
	 * @return boolean
	 */
	public static String readFile(String filePathAndName) {
		String result = "";
		StringBuffer sb = new StringBuffer();
		FileInputStream fis = null;
		InputStreamReader read = null;
		BufferedReader reader = null;
		try {
			File file = new File(filePathAndName);
			if (file.isFile() && file.exists()) {
				fis = new FileInputStream(file);
				read = new InputStreamReader(fis, "UTF-8");
				reader = new BufferedReader(read);
				String line = "";
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				result = sb.toString();
				sb.setLength(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
					reader = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (read != null) {
					read.close();
					read = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fis != null) {
					fis.close();
					fis = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 读SQL文件内容
	 * @param fileName
	 * @return
	 */
	public static String readSQLFile(File file) {
		String result = "";
		StringBuffer sb = new StringBuffer();
		FileInputStream fis = null;
		InputStreamReader read = null;
		BufferedReader reader = null;
		try {
			if (file != null && file.isFile() && file.exists()) {
				fis = new FileInputStream(file);
				read = new InputStreamReader(fis, "UTF-8");
				reader = new BufferedReader(read);
				String line;
				boolean ck = false;
				while ((line = reader.readLine()) != null) {
					if(ck){//第一行忽略
						if (line != null && !line.toString().contains("--")) {//没注释的
							sb.append(line + " ");
						}else if (line != null && line.toString().contains("--")) {//有注释的
							if(line.split("--").length>0 && line.split("--")[0].trim().length()>1){
								sb.append(line.split("--")[0] + " ");
							}
						}
					}
					ck = true;
				}
				result = sb.toString();
				sb.setLength(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
					reader = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (read != null) {
					read.close();
					read = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fis != null) {
					fis.close();
					fis = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 读TXT文件内容
	 * @param fileName
	 * @return
	 */
	public static String readTxtFile(File file) {
		String result = "";
		StringBuffer sb = new StringBuffer();
		InputStreamReader fileReader = null;
		BufferedReader bufferedReader = null;
		try {
			if (file!=null && file.isFile() && file.exists()) {
				//fileReader = new FileReader(file);
				fileReader = new InputStreamReader(new FileInputStream(file), "UTF-8");
				bufferedReader = new BufferedReader(fileReader);
				String read = null;
				while ((read = bufferedReader.readLine()) != null) {
					sb.append(read);
				}
				result = sb.toString();
				sb.setLength(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
					bufferedReader = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fileReader != null) {
					fileReader.close();
					fileReader = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 写入文件
	 * @param content
	 * @param fileName
	 * @throws Exception
	 */
	public static boolean writeTxtFile(String content, File file) {
		boolean flag = false;
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter writer = null;
		try {
			fos = new FileOutputStream(file);
			osw = new OutputStreamWriter(fos, "UTF-8");
			writer = new BufferedWriter(osw);
			writer.write(content);
			writer.flush();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (osw != null) {
					osw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	/**
	 * 重新写文件
	 * @param filePath
	 * @param content
	 */
	public static void contentToTxt(String filePath, String content) {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter writer = null;
		try {
			File file = new File(filePath);
			if (!(file.isFile() && file.exists())) {
				file.createNewFile();// 文件不存在则创建
			}
			fos = new FileOutputStream(file);
			osw = new OutputStreamWriter(fos, "UTF-8");
			writer = new BufferedWriter(osw);
			writer.write(content);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (osw != null) {
					osw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用DOM解析器来读取XML文件并生成SQL建表语句。以下是一个示例代码: ```java import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class XmlToSql { public static void main(String[] args) { try { // 读取XML文件 File inputFile = new File("input.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); // 获取所有"table"节点 NodeList tableList = doc.getElementsByTagName("table"); // 遍历"table"节点 for (int i = 0; i < tableList.getLength(); i++) { Node tableNode = tableList.item(i); if (tableNode.getNodeType() == Node.ELEMENT_NODE) { Element tableElement = (Element) tableNode; // 获取表名 String tableName = tableElement.getAttribute("name"); // 生成建表语句 StringBuilder sb = new StringBuilder(); sb.append("CREATE TABLE ").append(tableName).append(" ("); // 获取所有"column"节点 NodeList columnList = tableElement.getElementsByTagName("column"); // 遍历"column"节点 for (int j = 0; j < columnList.getLength(); j++) { Node columnNode = columnList.item(j); if (columnNode.getNodeType() == Node.ELEMENT_NODE) { Element columnElement = (Element) columnNode; // 获取列名和数据类型 String columnName = columnElement.getAttribute("name"); String dataType = columnElement.getAttribute("type"); // 添加列到建表语句中 sb.append(columnName).append(" ").append(dataType).append(", "); } } // 去除最后一个逗号和空格 sb.setLength(sb.length() - 2); // 添加主键 sb.append(", PRIMARY KEY (id)"); // 结束建表语句 sb.append(");"); // 输出建表语句 System.out.println(sb.toString()); } } } catch (Exception e) { e.printStackTrace(); } } } ``` 假设有一个名为"input.xml"的XML文件,内容如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <database> <table name="users"> <column name="id" type="INT"/> <column name="name" type="VARCHAR(50)"/> <column name="email" type="VARCHAR(50)"/> </table> <table name="posts"> <column name="id" type="INT"/> <column name="title" type="VARCHAR(50)"/> <column name="content" type="TEXT"/> </table> </database> ``` 运行上面的Java代码,将会输出以下内容: ``` CREATE TABLE users (id INT, name VARCHAR(50), email VARCHAR(50), PRIMARY KEY (id)); CREATE TABLE posts (id INT, title VARCHAR(50), content TEXT, PRIMARY KEY (id)); ``` 这就是通过Java代码读取XML文件并生成SQL建表语句的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值