Selam arkadaşlar,
Elimizde bir matrix var ve bu matrix bir dosyada kayıtlı(orjinal olarak excelde). Bu matrix i java da 2 boyutlu bir array e atmam gerekiyor. Text file dan veri okumak için aşağıda ki gibi bir kod buldum fakat içindeki değerleri array e atamadım. Excel den okuyabilir miyim bilmediğim için text dosyasından alt alta alıp array e atmak istiyorum fakat beceremedim
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class FileInput {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
File file = new File("D:\\temp.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
int [][] array1 = new int[10][10];
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
System.out.println(dis.readLine());
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
kod bu şekilde bu ne eklersem text dosyasında ki verileri array e atabilirim. Bir de direk excel den almanın bir yolu var mıdır. Şimdiden teşekkürler.
Elimizde bir matrix var ve bu matrix bir dosyada kayıtlı(orjinal olarak excelde). Bu matrix i java da 2 boyutlu bir array e atmam gerekiyor. Text file dan veri okumak için aşağıda ki gibi bir kod buldum fakat içindeki değerleri array e atamadım. Excel den okuyabilir miyim bilmediğim için text dosyasından alt alta alıp array e atmak istiyorum fakat beceremedim
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class FileInput {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
File file = new File("D:\\temp.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
int [][] array1 = new int[10][10];
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
System.out.println(dis.readLine());
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
kod bu şekilde bu ne eklersem text dosyasında ki verileri array e atabilirim. Bir de direk excel den almanın bir yolu var mıdır. Şimdiden teşekkürler.

while (dis.available() != 0) {
}
var ya hani, onun içinde gerçekleştireceksin her şeyi.
Öncelikle verideki sayılar nasıl ayrılıyor, boşlukla mı? Öyle varsayıyorum.
String[] line = dis.readLine().split(" ");
diyerek satırı alıyoruz ve " " ile bölüyoruz ve bize bir adet string arrayi yaratıyor.
Sonra bir döngü yaratarak tek tek o Stringleri integer'a çevirip matrisin kolonlarına yerleştiriyoruz.
array[i][j] = Integer.parseInt(line[j]); gibi bir şeyle.
her satır atladığında da i'yi arttırıyoruz.
Kodu daha hızlı yazardım aslında da sizin işinizi/ödevinizi yapmak etik olmaz diye düşündüm :).
Excel'den okumak için bazı frameworkler var ama çok kafa karıştırır. csv'yi araştırabilirsiniz.
natnan

1