方案:我有调用Soap Web服务的代码,得到的附件是zip文件。然后将其解压缩,浏览所有文件,获取我想要的一个文件,即csv文件,并获取csv文件的内容:
public static void unzipTry2(AttachmentPart att) throws IOException, SOAPException {
try (ZipInputStream zis = new ZipInputStream(att.getRawContent())) {
byte[] buffer = new byte[1024];
for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry = zis.getNextEntry()) {
if (zipEntry.isDirectory()) {
continue;
}
if (!zipEntry.getName().equals("FileIwant.csv")) {
continue; //if it's not the file I want, skip this file
}
System.out.println(zipEntry.getName());
for (int len = zis.read(buffer); len > 0; len = zis.read(buffer)) {
//System.out.write(buffer, 0, len);
String testString = new String(buffer,0,len);
processCSVString(testString);
}
}
}
}
效果很好。但是,我得到的CSV文件仅包含一行,现在应该可以,但是将来可能包含多行。由于它是CSV文件,因此我需要逐行解析。此代码还必须适用于CSV文件包含多行的情况,这就是我不确定它是否有效的地方,因为没有办法测试(我不控制此方法的输入,来自网络服务)。
您能告诉我内部for循环是否逐行读取LINE文件吗? :
for (int len = zis.read(buffer); len > 0; len = zis.read(buffer)) {
//System.out.write(buffer, 0, len);
String testString = new String(buffer,0,len);
processCSVString(testString);
}
答案 0 :(得分:0)
BufferedReader
是Java的“事物”,可以逐行读取Reader
。而您需要的粘合剂是InputStreamReader
。然后,您可以将/*
This Query is built for a 4-4-5 calendar, Mon - Sun week.
Each Fiscal year start on 01/01/YYYY ends on 12/31/YYYY.
*/
DECLARE
@FiscalCalendarStart DATETIME, -- Calendar starts
@EndOfCalendar DATETIME, -- Calendar ends
@CurrentDate DATETIME, -- Calendar date being calculated against
@RunningDaySeed INT, -- Days calculations
@RunningWeekSeed INT, -- Weeks calculations
@FiscalYearSeed INT, -- Years calculations
@RunningPeriodSeed INT, -- Fiscal Month
@WorkWeekSeed INT, -- Fiscal Week
@WorkQuarterSeed INT, -- Fiscal Quarter
@WorkPeriod INT, -- Used to assign where the extra "leap week" goes. Based on the 4-4-5 calendar.
@WeekEnding DATETIME
DECLARE @FiscalTimePeriods AS TABLE (
Sysdate VARCHAR (10), -- Date with format MMDDYYYY
Date_Time_Target DATETIME, -- The date, but with Time duh
MSC_Week INT, -- Running week according MSC Calendar
MSC_Year INT, -- Calendar with corresponding days into the Fiscal year
weekEnding DATETIME,
runningWeekSeed INT
)--Of Table
/* These are user variables, and should be set according every need */
SELECT
@FiscalCalendarStart = '20150101', -- The date of year start. Used as the base date for calculations (This case will be on 12/29/2015 to match 7 days week for week 1 of 2015)
@EndOfCalendar = '20901231' , -- The date on which the calendar query will end. Can be adjusted for each need
@RunningDaySeed = 1, -- This is used to measure the number of days since the calendar began, often used for depreciation (usually 1)
@RunningPeriodSeed = 1, -- The number of fiscal months since the original calendar began (usually 1)
@RunningWeekSeed = 1, -- The number of fiscal weeks since the original calendar began (usually 1)
@FiscalYearSeed = 2015 -- The starting fiscal year (first 3 days of week 1 belongs to 2014 but it's on 2015 fiscal calendar)
/* These are iteration variables, do not mess with these */
SELECT
@WorkPeriod = 1 ,
@WorkWeekSeed = 1 ,
@WorkQuarterSeed = 1
/* The loop is iterated once for each day */
SET @CurrentDate = @FiscalCalendarStart
SELECT @WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate )
WHILE @CurrentDate <= @EndOfCalendar
BEGIN --MAIN BEGIN
INSERT INTO @FiscalTimePeriods
SELECT
CONVERT ( VARCHAR (10), @CurrentDate , 101 ) AS SysDate,
@CurrentDate AS Date_Time_Target,
@WorkWeekSeed AS MSC_Week,
YEAR (@CurrentDate) AS MSC_Year,
@WeekEnding,
@RunningWeekSeed
/* Iterate the date and increment the RunningDay */
SET @CurrentDate = DATEADD ( D , 1 , @CurrentDate )
SELECT @RunningDaySeed = @RunningDaySeed + 1,
@WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate )
/* If @CurrentDate is 01/01/YYYY, reset fiscal counters */
IF (DATEPART (MONTH, @CurrentDate) = 1 AND DATEPART(DAY, @CurrentDate) = 1)
BEGIN
SELECT
@WorkPeriod = 1 ,
@WorkWeekSeed = 1 ,
@WorkQuarterSeed = 1,
@FiscalYearSeed = @FiscalYearSeed + 1
END
ELSE
/*************Check if Monday is between 12/23 to 12/31 *****************/
IF DATEPART ( DW , @CurrentDate ) = 2 -- Check if @CurrentDate is monday
IF DATEPART (MONTH, @CurrentDate) = 12 AND (DATEPART(DAY, @CurrentDate) in (23, 24, 25, 26, 27, 28, 29, 30, 31)) -- If @CurrentDate is Dec 23 to 31
BEGIN
SELECT
@WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate ),
@WorkWeekSeed = 52,
@WorkQuarterSeed = 4,
@WorkPeriod = 12
END
ELSE
IF DATEPART ( DW , @CurrentDate ) = 2
BEGIN
SELECT
@WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate ),
@WorkWeekSeed = CASE
WHEN @WorkWeekSeed = 53 THEN 52
ELSE @WorkWeekSeed + 1
END
END
/* Compare if current date acomplish ISO_WEEK standard ISO 8601*/
IF DATEPART ( ISO_WEEK , @CurrentDate ) = 1
SELECT
@WorkPeriod = 1,
@WorkWeekSeed = 1,
@WorkQuarterSeed = 1
------------------------HERE IS WHERE START TO DO THE CALCULATION OF THE runningWeekSeed-------------------------
IF @WorkWeekSeed = 52
SELECT @WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate ),
@RunningWeekSeed = @RunningWeekSeed
ELSE
IF DATEPART(DW, @CurrentDate) = 2
SELECT @RunningWeekSeed = @RunningWeekSeed + 1
ELSE
IF (DATEPART (MONTH, @CurrentDate) = 1 AND DATEPART(DAY, @CurrentDate) = 1)
SELECT @RunningWeekSeed = @RunningWeekSeed + 1
END -- MAIN BEGIN
SELECT
*
FROM @FiscalTimePeriods
ORDER BY Date_Time_Target ASC
包装为
ZipInputStream
(最好在try-with-resources块中),从BufferedReader br=new BufferedReader(new InputStreamReader(zis))
读取的经典循环如下所示:
BufferedReader