Java'da Veri İçe Aktarma
Anthony Markham
VP Quant Developer
InputStream/OutputStream)Reader/Writer)
InputStream (okuma), OutputStream (yazma)FileInputStream, FileOutputStreamread() bayt döndürür, write() bayt yazar
try (FileInputStream fis = new FileInputStream("data.bin")) {byte[] buffer = new byte[1024]; int bytesRead;while ((bytesRead = fis.read(buffer)) != -1) { // Tampondaki baytları işleyinSystem.out.println("Okunan " + bytesRead + " bayt"); }} catch (Exception e) { System.err.println("Hata: " + e.getMessage()); }
Reader (metin okuma), Writer (metin yazma)FileReader, FileWriter, BufferedReader, BufferedWriterreadLine() tam bir metin satırı okur
try (BufferedReader reader = new BufferedReader(new FileReader("data.csv"))) { String line;while ((line = reader.readLine()) != null) { // Her satırı işleyin System.out.println(line); }} catch (Exception e) { System.err.println("Hata: " + e.getMessage()); }
BufferedInputStream ve BufferedOutputStream ile bayt akışlarını okuyun/yazınBufferedReader, BufferedWriter ile karakter akışlarını okuyun/yazınreadLine() ile bir satırı tamamen okuyuntry (BufferedReader reader = new BufferedReader(new FileReader("data.csv"))) { // Başlık işleme String header = reader.readLine(); System.out.println("Başlık: " + header);// Verimli veri okuma String line; int count = 0; while ((line = reader.readLine()) != null) { count++; } System.out.println("Okunan " + count + " veri satırı"); } catch (Exception e) { System.out.println("Hata: " + e.getMessage()); }
FileInputStream -> InputStreamReader -> BufferedReaderimport java.nio.charset.StandardCharsets;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("data.csv"),StandardCharsets.UTF_8 ))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line);} } catch (Exception e) { System.err.println("Hata: " + e.getMessage()); }
IOException, FileNotFoundExceptionimport java.util.ArrayList;
import java.util.List;
public static List<String> readLines(String filePath) throws IOException {List<String> lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { lines.add(line); } } // Akış burada otomatik olarak kapanır return lines;}
Java'da Veri İçe Aktarma