java读取Csv格式文件

该博客展示了如何使用Java的BufferedReader和FileReader读取CSV文件。通过split()函数以逗号为分隔,将每一行数据存储到List<String[]>中,便于后续处理。示例代码成功读取了'Sharedbikezjut-sample.csv'文件,并打印出部分订单的公司名、出发时间和到达时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java读取Csv格式文件

1.用BufferedReader方法来读取文件
2.spilt()函数以逗号为分割将整行分割,得到一个数组
3.为了后期方便调用函数,我用List进行存储,List<String[]>即为返回值

package 课设.readCsv;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadCsv {
	/* 读取csv文件的方法 
	 * 主要函数是ReadMethod()
	 * 放回类型是List<String[]> 一个List的集合 每一个元素是包含数条信息的数组 
	 * (MsgID/CompanyId/BicycleNo/OrderId/DepartTime/DepartLongitude/DepartLatitude/ArriveTime/ArriveLongitude/ArriveLatitude/DBTime)
	 */
	List<String[]> ReadMethod(){
		String csvFile = "Sharedbikezjut-sample.csv";
        String line = "";
        String cvsSplitBy = ",";
        int count = 0;
        String[] Line = new String[1];
        List<String[]> BikeDataList = new ArrayList<String[]>();
        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
 
            while ((line = br.readLine()) != null) {
            	Line = line.split(cvsSplitBy);//将逗号作为分隔符 划分一行 得到数组[]
            	BikeDataList.add(Line);
               // System.out.println("Order:"+(++count)+" Company:"+Line[1]+" DepartTime: "+Line[4]+ "  ArriveTime:"+Line[7]);
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return BikeDataList;
	}
    public static void main(String[] args) {
    	 new ReadCsv().ReadMethod();
    }
}

跑出来的结果如下:

Order:29979 Company:hellobike DepartTime: 2018/3/20 9:38  ArriveTime:2018/3/20 9:43
Order:29980 Company:hellobike DepartTime: 2018/3/20 16:03  ArriveTime:2018/3/20 16:09
Order:29981 Company:hellobike DepartTime: 2018/3/20 16:18  ArriveTime:2018/3/20 16:34
Order:29982 Company:hellobike DepartTime: 2018/3/20 18:09  ArriveTime:2018/3/20 18:14
Order:29983 Company:hellobike DepartTime: 2018/3/20 15:26  ArriveTime:2018/3/20 15:27
Order:29984 Company:hellobike DepartTime: 2018/3/20 14:58  ArriveTime:2018/3/20 15:05
Order:29985 Company:hellobike DepartTime: 2018/3/20 11:38  ArriveTime:2018/3/20 11:45
Order:29986 Company:hellobike DepartTime: 2018/3/20 7:25  ArriveTime:2018/3/20 7:33
Order:29987 Company:hellobike DepartTime: 2018/3/20 7:37  ArriveTime:2018/3/20 7:46
Order:29988 Company:hellobike DepartTime: 2018/3/20 15:58  ArriveTime:2018/3/20 16:05
Order:29989 Company:hellobike DepartTime: 2018/3/20 13:04  ArriveTime:2018/3/20 13:09
Order:29990 Company:hellobike DepartTime: 2018/3/20 17:12  ArriveTime:2018/3/20 17:20
Order:29991 Company:hellobike DepartTime: 2018/3/20 11:38  ArriveTime:2018/3/20 11:55
Order:29992 Company:hellobike DepartTime: 2018/3/20 14:46  ArriveTime:2018/3/20 14:51
Order:29993 Company:hellobike DepartTime: 2018/3/20 9:45  ArriveTime:2018/3/20 9:54
### SWUST OJ Problem 32 Information and Solution Unfortunately, specific details about SWUST OJ problem number 32 are not directly provided in the available references. However, based on similar problems from this platform such as those mentioned in other citations, a general approach to solving typical programming challenges can be outlined. #### Understanding Common Elements of Programming Problems on SWUST OJ Platform Problems like SWUSTOJ276, SWUSTOJ77, SWUSTOJ78, SWUSTOJ1286, and SWUSTOJ1285 emphasize proper use of `if` and `else` statements along with maintaining good coding practices including appropriate formatting[^1]. For instance, when dealing with numerical outputs, `%g` is used for automatic selection between fixed-point notation (`%f`) or scientific notation (`%e`), depending on which provides more compact output without loss of precision. Given that detailed specifics regarding problem 32 aren't present here, one should look at common patterns found across different types of questions posed by platforms like these: - **Input Handling**: Typically involves reading inputs either single values or arrays/lists. - **Logic Implementation**: Applying algorithms ranging from simple arithmetic operations up through complex data structures manipulation. - **Output Formatting**: Ensuring results adhere strictly to specified formats using placeholders like `%d`, `%s`, etc., where applicable. Since no direct reference exists specifically addressing SWUST OJ problem 32 within given sources, consider exploring adjacent numbered problems around it for clues about its nature—whether mathematical computation, string processing, dynamic programming elements, et cetera—and adapt solutions accordingly while keeping best practice guidelines intact. ```c // Example C code snippet demonstrating basic structure often seen in contest-style programs #include <stdio.h> int main() { int n; scanf("%d", &n); // Read input value if (condition_based_on_problem_statement) { printf("Result under condition A\n"); } else { printf("Alternative result\n"); } return 0; } ``` --related questions-- 1. How does understanding how `%g` works help improve program efficiency? 2. What strategies could apply towards optimizing performance in competitive programming contests? 3. Can you provide examples illustrating effective usage of conditional operators (`if`, `else`) in algorithm design?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值