我已创建此代码,但不知道为什么它不起作用。代码不会打印csvfile的所有行。
try{
File csvfile = new File(FullPath);
FileInputStream csvStream = new FileInputStream(csvfile);
BufferedReader in = new BufferedReader(new InputStreamReader(csvStream));
String line;
int iCount=0;
while ((line = in.readLine()) != null){
String[] RowData = line.split(",");
name[iCount] = RowData[0];
Toast.makeText(NewMessage.this, "CSV", 2000).show();
number[iCount] = RowData[1];
iCount++;
}
in.close();
Toast.makeText(NewMessage.this, "CSV Has uploaded", 2000).show();
}
catch(Exception e)
{
e.printStackTrace();
}
答案 0 :(得分:0)
如果问题是没有打印文件的所有行,则在循环中覆盖数组。你应该编辑你的问题,因为数组正在运行,真正的问题是文件没有打印所有行。
以下是名称,数字格式的解决方案:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Teste {
public static void main(String[] args) throws IOException {
Teste x = new Teste();
x.doTeste();
}
public void doTeste() throws IOException{
String fileName = "D:\\Projetos\\Testes\\src\\teste.txt";
BufferedReader in = null;
try{
File csvfile = new File(fileName);
FileInputStream csvStream = new FileInputStream(csvfile);
in = new BufferedReader(new InputStreamReader(csvStream));
String line;
String[] rowName = new String[(countLines(fileName)+1)];
String[] rowNameData = new String[(countLines(fileName)+1)];
int linha = 0;
while ((line = in.readLine()) != null){
String[] array = line.split(",");
rowName[linha] = array[0];
rowNameData[linha] = array[1];
++linha;
}
System.out.println("The rowName ARRAY");
for (String s: rowName){
System.out.println(s);
}
System.out.println("The rowNameData ARRAY");
for (String s: rowNameData){
System.out.println(s);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
in.close();
}
}
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
}
并在一行中输入名称,并在下一行编号:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Teste {
public static void main(String[] args) throws IOException {
Teste x = new Teste();
x.doTeste();
}
public void doTeste() throws IOException{
String fileName = "D:\\Projetos\\Testes\\src\\teste.txt";
BufferedReader in = null;
try{
File csvfile = new File(fileName);
FileInputStream csvStream = new FileInputStream(csvfile);
in = new BufferedReader(new InputStreamReader(csvStream));
String line;
String[] rowName = new String[(countLines(fileName)+1)/2];
String[] rowNameData = new String[(countLines(fileName)+1)/2];
int iCount=0;
int x = 0;
int y = 0;
while ((line = in.readLine()) != null){
if (iCount == 0 || iCount%2 == 0) {
rowName[x] = line;
++x;
}
else {
rowNameData[y] = line;
++y;
}
iCount++;
}
System.out.println("The rowName ARRAY");
for (String s: rowName){
System.out.println(s);
}
System.out.println("The rowNameData ARRAY");
for (String s: rowNameData){
System.out.println(s);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
in.close();
}
}
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
}