java 操作 CSV 文件,纯贴代码

其实是一个公司的笔试题,上网查查,发现网上的代码都是不完全正确的。没有考虑到一些极端的例子。贴段代码,方便自己以后查看。

主要是分析csv文件内容比较麻烦,特别是出来引号中的逗号。其他的都没什么。

package com.ltm.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CsvHandler {

	private List<List<String>> result;

	CsvHandler() {
		result = new ArrayList<List<String>>();
	}

	public void readerCSV(InputStream is) throws IOException {
		String filePath = "H:\\pg\\test.csv";
		BufferedReader in = new BufferedReader(new FileReader(filePath));
		String str = "";
		while ((str = in.readLine()) != null) {
			List<String> r = new ArrayList<String>();
			str = decodeString(str);
			String[] s = str.split(",");
			for (int i = 0; i < s.length; i++) {
				String temp = s[i].replace("|", ",");
				if (temp.startsWith("\"")) {
					temp = temp.substring(1, temp.length() - 1);
				}
				temp = temp.replace("\"\"", "\"");
				r.add(temp);
				// System.out.println(temp);
			}
			result.add(r);
		}
		in.close();
	}

	/**
	 * 用户退出时自动将内存里的数据写入服务器文件中或者直接提示用户下载
	 * 
	 * @param savePath
	 * @throws IOException
	 */
	public void closeCSV() throws IOException {
		SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmm");
		Date today = new Date();
		String filename = dataFormat.format(today);
		File file = new File(filename+".csv");
		if (!file.exists()){
			file.createNewFile();
		}
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		String temp = new String();
		for (List<String> s : result) {
			for (String str : s) {
				temp += encodeString(str) + ",";
			}
			temp = temp.substring(0, temp.length() - 1);
			out.write(temp);
			temp = "";
			out.newLine();
		}
		out.flush();
		out.close();
	}

	public void editCsv(int row, int col, String s) {
		this.result.get(row).set(col, s);
	}

	public void addDate(String[] s) {
		List<String> e = new ArrayList<String>();
		for (String st : s) {
			e.add(st);
		}
		this.result.add(e);
	}

	public static String encodeString(String str) {
		if (str.indexOf("\"") != -1 || str.indexOf(",") != -1) {
			str = "\"" + str.replace("\"", "\"\"") + "\"";
		}
		return str;
	}

	/**
	 * 将""中的,转变为|
	 * 
	 * @param args
	 */
	public static String decodeString(String str) {
		int a = 0;
		int b = 0;
		int c = 0;
		a = str.indexOf("\"");
		while (a != -1) {
			c = str.indexOf(",", a);
			b = str.indexOf("\"", a + 1);
			if (c == -1)
				break;
			if (a < c && c < b) {
				String temp = str.substring(a + 1, b);
				temp = temp.replace(",", "|");
				str = str.substring(0, a + 1) + temp
						+ str.substring(b, str.length());
			}
			a = str.indexOf("\"", b + 1);
		}
		return str;
	}

	public static void main(String[] args) {
		CsvHandler cs = new CsvHandler();
		try {
			String[] a = { "sfds,df", "\"hi\"", "hibaby" };
			cs.readerCSV(null);
			cs.addDate(a);
			cs.closeCSV();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 获取列数
	public int getCol() {
		int temp = 0;
		for (int i = 0; i < this.result.size(); i++) {
			if (temp < this.result.get(i).size()) {
				temp = this.result.get(i).size();
			}
		}
		return temp;
	}

	// 获取最大的行数
	public int getRow() {
		return this.result.size();
	}

}
 
智能网联汽车的安全员高级考试涉及多个方面的专业知识,包括但不限于自动驾驶技术原理、车辆传感器融合、网络安全防护以及法律法规等内容。以下是针对该主题的些核心知识点解析: ### 关于智能网联车安全员高级考试的核心内容 #### 1. 自动驾驶分级标准 国际自动机工程师学会(SAE International)定义了六个级别的自动驾驶等级,从L0到L5[^1]。其中,L3及以上级别需要安全员具备更高的应急处理能力。 #### 2. 车辆感知系统的组成与功能 智能网联车通常配备多种传感器,如激光雷达、毫米波雷达、摄像头和超声波传感器等。这些设备协同工作以实现环境感知、障碍物检测等功能[^2]。 #### 3. 数据通信与网络安全 智能网联车依赖V2X(Vehicle-to-Everything)技术进行数据交换,在此过程中需防范潜在的网络攻击风险,例如中间人攻击或恶意软件入侵[^3]。 #### 4. 法律法规要求 不同国家和地区对于无人驾驶测试及运营有着严格的规定,考生应熟悉当地交通法典中有关自动化驾驶部分的具体条款[^4]。 ```python # 示例代码:模拟简单决策逻辑 def decide_action(sensor_data): if sensor_data['obstacle'] and not sensor_data['emergency']: return 'slow_down' elif sensor_data['pedestrian_crossing']: return 'stop_and_yield' else: return 'continue_driving' example_input = {'obstacle': True, 'emergency': False, 'pedestrian_crossing': False} action = decide_action(example_input) print(f"Action to take: {action}") ``` 需要注意的是,“橙点同学”作为特定平台上的学习资源名称,并不提供官方认证的标准答案集;建议通过正规渠道获取教材并参加培训课程来准备此类资格认证考试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值