写csv相关操作

std::ios::in:输入模式,表示以读取方式打开流。
std::ios::out:输出模式。
std::ios::app:追加模式,表示以写入方式打开流,并将数据追加到文件末尾而不清除原有内容。
std::ios::trunc:截断模式,表示以写入方式打开流,并清除文件原有内容。
std::ios::binary:二进制模式,以二进制方式打开流。
std::ios::ate:打开流后将文件指针定位到文件末尾。

	//写csv  覆盖
	if (0) {
		std::ofstream outFile;
		outFile.open("t444est.csv", std::ios::out | std::ios::trunc);
		// 写入标题行



		outFile << "name" << ','
			<< "income" << ','
			<< "expenditure" << ','
			<< "addr" << std::endl;
		// ********写入两行数据*********
		// 写入字符串(数字)
		outFile << "zhangsan" << ','
			<< "3000" << ','
			<< "1200" << ','
			<< "中国 北京市" << std::endl;
		// 写入浮点数(转为字符串)
		outFile << "lisi" << ','
			<< std::to_string(2032.1) << ','
			<< std::to_string(789.2) << ','
			<< "中国 陕西省13333311" << std::endl;
		std::string name1="1222222";
		std::string name2="哈哈";
		std::string name3="asd好";
		CString name4;
		name4 = _T("号1111sad");
		outFile << name1 << ',';
		outFile << name2 << ',';
		outFile << name3 << ',';
		outFile << (CW2A)name4 << std::endl;



		outFile.close();
	}

	//写csv  不覆盖
	if (1) {
		std::ofstream outFile;
		outFile.open("t2est.csv", std::ios::app);
		// 写入标题行

		int a = 977676;

		outFile << "name" << ','
			<< "income" << ','
			<< "expenditure" << ','
			<< "addr" << std::endl;
		// ********写入两行数据*********
		// 写入字符串(数字)
		outFile << "" << ',' << "asfasf"
			<< a << ','
			<< "1200" << ','
			<< "中国"; 
		outFile << "北京asdasfasfsa市" << std::endl;
		// 写入浮点数(转为字符串)
		outFile << "lisi" << ','
			<< std::to_string(2032.1) << ','
			<< std::to_string(789.2) << ','
			<< "中国 陕西省13333311" << std::endl;
		std::string name1 = "1222222";
		std::string name2 = "哈哈";
		std::string name3 = "asd好";
		std::string name4 = "号sad";
		outFile << name1 << ',';
		outFile << name2 << ',';
		outFile << name3 << ',';
		outFile << name4 << std::endl;



		outFile.close();
	}



CSV(Comma-Separated Values)文件是一种常见的文本文件,其内容是由逗号分隔的值组成的数据。在编程中,读CSV文件是一种基础的文件操作。下面将分别介绍在Java中如何读CSV文件以及异常处理的相关内容。 ### 读取CSV文件 读取CSV文件通常会涉及到文件的打开、读取以及关闭操作。以下是使用Java进行这些操作的基本步骤: 1. 创建`BufferedReader`对象,用于从文件中读取数据。 2. 使用循环和`readLine()`方法逐行读取文件。 3. 对于每一行,可以使用`String.split(",")`方法按逗号分隔符进行分割,得到字符串数组,其中每个元素代表一列数据。 4. 对读取的数据进行处理。 5. 关闭`BufferedReader`对象。 ### CSV文件 CSV文件的过程与读取类似,但方向相反: 1. 创建`PrintWriter`对象,用于向文件入数据。 2. 使用循环逐个入数据,可以使用`print()`和`println()`方法。 3. 对于每个操作,数据之间可以添加逗号作为分隔符,每完一行数据后使用`println()`换行。 4. 关闭`PrintWriter`对象。 ### 异常处理 在文件操作中,可能会遇到多种异常情况,如文件不存在、没有读权限等。因此,异常处理是非常重要的一部分。在Java中,可以使用`try-catch`语句块来处理可能发生的异常。 ### 示例代码 ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class CSVFileIO { public static void readCSV(String filePath) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(filePath)); String line = null; while ((line = reader.readLine()) != null) { String[] values = line.split(","); // 处理每行的数据 } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void writeCSV(String filePath, String[][] data) { PrintWriter writer = null; try { writer = new PrintWriter(new FileWriter(filePath)); for (String[] row : data) { for (int i = 0; i < row.length; i++) { writer.print(row[i]); if (i < row.length - 1) { writer.print(","); } } writer.println(); } } catch (IOException e) { e.printStackTrace(); } finally { if (writer != null) { writer.close(); } } } } ``` 在这个示例中,`readCSV`方法展示了如何读取CSV文件,`writeCSV`方法展示了如何入数据到CSV文件。异常处理则是通过`try-catch-finally`块来实现的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值