for (i = 1; i <=8; i++) { if ( i == 1 || i > 5 ){ fileName = tempDir + "/page_" + i + ".xls"; } File file = new File(fileName); OutputStream outputStream = null; try { outputStream = new FileOutputStream(file,true); workbook.write(outputStream); } finally { FileKit.closeOutStream(outputStream); } }以上代码如何修改可保证i=2、3、4时插入的数据是在i=1时创建的文件里
时间: 2024-03-10 16:47:31 浏览: 20
要实现i=2、3、4时插入的数据在i=1时创建的文件里,可以将文件名的生成放到循环外面,并且将文件对象的创建放到循环外面,然后在循环体内根据不同的i值,将数据写入不同的文件中。代码如下:
```
// 生成文件名
String fileName = tempDir + "/page.xls";
// 创建文件对象
File file = new File(fileName);
// 创建输出流对象
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file, true);
// 循环写入数据
for (int i = 1; i <= 8; i++) {
// 根据不同的i值,将数据写入不同的文件
if (i == 1 || i > 5) {
workbook.write(outputStream);
}
}
} finally {
FileKit.closeOutStream(outputStream);
}
```
这样修改后,可以保证i=2、3、4时插入的数据在i=1时创建的文件里。
相关问题
for(i=2;i<MSD+2;i++) if (!strcmp(tempDir->directItem[i].fileName, fName) && tempDir->directItem[i].type == 1) break;
这是一段 C 语言代码,其中使用了一个 for 循环和一个 if 判断语句。循环的条件是 i<MSD+2,意思是 i 从 2 开始一直循环到 MSD+1。在循环体内部,使用了 strcmp 函数比较字符串是否相等,并且判断了一个文件类型是否为 1。如果条件成立,就会跳出循环,否则继续循环。请问你需要我解释这段代码的具体作用吗?
#include <iostream> #include <fstream> #include <string> #include<stdlib.h> #include<algorithm> using namespace std; int main() { fstream datafile; int i; string filename,line[100],name[100],searchname,name1[100]; cout << "Enter the FULL name of the file: "; cin >> filename; datafile.open(filename.c_str(), fstream::in); if (datafile.is_open() == false) { cout << "ERROR: not able to open " << filename << endl; } else { int number = 0; while (getline(datafile, line[number])) { number++;} cout<<"enter the name you search:"; cin>>searchname; for(int i=0;i<number;i++) { int sea=line[i].find(searchname); if(sea==-1) { cout<<"error:do not find this name"<<endl; }else { while(line[i]!=";") { name1[i]=line[i]; } int mao=line[i].find(';'); int total=line[i].length(); if(name1[i]==searchname) { cout<<line[i].substr(mao+1,total); } } } datafile.close(); } system("pause"); return 0; } 这段代码有什么问题 如何改正
这段代码的问题在于:
1. 在循环中没有对name1数组进行初始化,可能会导致出现未定义的行为。
2. 在找到搜索名字后,没有对name1数组进行更新。
3. 在找到搜索名字后,直接输出line[i]后面的内容,而不是输出name1[i]后面的内容。
4. 在读取文件名时,应该使用getline(cin, filename)而不是cin>>filename,以避免在文件名中包含空格时出现问题。
改正后的代码如下:
```
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int main() {
fstream datafile;
int i;
string filename, line[100], name[100], searchname, name1[100];
cout << "Enter the FULL name of the file: ";
getline(cin, filename);
datafile.open(filename.c_str(), fstream::in);
if (datafile.is_open() == false) {
cout << "ERROR: not able to open " << filename << endl;
} else {
int number = 0;
while (getline(datafile, line[number])) {
number++;
}
cout << "Enter the name you search: ";
cin >> searchname;
for (int i = 0; i < number; i++) {
int sea = line[i].find(searchname);
if (sea == -1) {
continue;
} else {
int j = 0;
while (line[i][j] != ';') {
name1[i] += line[i][j];
j++;
}
int mao = line[i].find(';');
int total = line[i].length();
if (name1[i] == searchname) {
cout << line[i].substr(mao + 1, total) << endl;
}
}
}
datafile.close();
}
system("pause");
return 0;
}
```