I'm trying to make an array from CSV file. I wtore this but I don't knew how to finish it.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Java {
public static void main (String[] argv) throws IOException {
File file = new File("1.txt");
Scanner fileReader = new Scanner(file);
System.out.printf(fileReader.nextLine());
FileReader.close();
}
}
read a CSV file in java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;
public class CsvFileReader {
public static void main(String[] args) {
try {
System.out.println("\n**** readLineByLineExample ****");
String csvFilename = "C:/Users/hussain.a/Desktop/sample.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
String[] col = null;
while ((col = csvReader.readNext()) != null)
{
System.out.println(col[0] );
//System.out.println(col[0]);
}
csvReader.close();
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae+" : error here");
}catch (FileNotFoundException e)
{
System.out.println("asd");
e.printStackTrace();
} catch (IOException e) {
System.out.println("");
e.printStackTrace();
}
}
}
and you can get the related jar file from here
You can do something like fileReader.nextLine().split(",")
to create an array of Strings representing the comma-delimited items in the line.
Or you can use StringTokenizer:
StringTokenizer st = new StringTokenizer(fileReader.nextLine(), ",");
and then iterate through the tokens to get the comma-delimited items from the line.
Scanner s = null;
List<String[]> list = new ArrayList<String[]>
try {
s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
while (s.hasNext()) {
String str = s.nextLine();
list.add(str .split(","));
}
} finally {
if (s != null) {
s.close();
}
}
list would have array of stings
File f = new File(filepath)
BufferedReader b = new Bufferedreader(f.getInputStream);
String line = "";
String[] myArray = new String[100];
while((line = b.readLine())!=null) // read file
{
myArray[count++] = line; // store in an array
}
I'm trying to make an array from CSV file. I wtore this but I don't knew how to finish it.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Java {
public static void main (String[] argv) throws IOException {
File file = new File("1.txt");
Scanner fileReader = new Scanner(file);
System.out.printf(fileReader.nextLine());
FileReader.close();
}
}
read a CSV file in java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;
public class CsvFileReader {
public static void main(String[] args) {
try {
System.out.println("\n**** readLineByLineExample ****");
String csvFilename = "C:/Users/hussain.a/Desktop/sample.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
String[] col = null;
while ((col = csvReader.readNext()) != null)
{
System.out.println(col[0] );
//System.out.println(col[0]);
}
csvReader.close();
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae+" : error here");
}catch (FileNotFoundException e)
{
System.out.println("asd");
e.printStackTrace();
} catch (IOException e) {
System.out.println("");
e.printStackTrace();
}
}
}
and you can get the related jar file from here
You can do something like fileReader.nextLine().split(",")
to create an array of Strings representing the comma-delimited items in the line.
Or you can use StringTokenizer:
StringTokenizer st = new StringTokenizer(fileReader.nextLine(), ",");
and then iterate through the tokens to get the comma-delimited items from the line.
Scanner s = null;
List<String[]> list = new ArrayList<String[]>
try {
s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
while (s.hasNext()) {
String str = s.nextLine();
list.add(str .split(","));
}
} finally {
if (s != null) {
s.close();
}
}
list would have array of stings
File f = new File(filepath)
BufferedReader b = new Bufferedreader(f.getInputStream);
String line = "";
String[] myArray = new String[100];
while((line = b.readLine())!=null) // read file
{
myArray[count++] = line; // store in an array
}
0 commentaires:
Enregistrer un commentaire