Soru & Cevap

Android Dosya İşlemleri ...

18.08.2017 - 01:43

Yeniden merhaba arkadaşlar. 

File myInternalFile = new File(context.getFilesDir(), fileName);

            if (myInternalFile.exists()) {
                //dosya varsa
            } else {

                myInternalFile.createNewFile();
            }

            FileOutputStream myOutputStream = new FileOutputStream(myInternalFile, true);
            OutputStreamWriter myOutputStreamWriter = new OutputStreamWriter(myOutputStream);

            for (int i = 0; i < data.length; i++) {
                myOutputStreamWriter.write("\n"+data[i]);

            }

            myOutputStreamWriter.flush();
            myOutputStreamWriter.close();

Şöyle bir kod denedim ve verileri dosyaya alt alta yazdırmaya çalıştım fakat olmadı. Hatam nerede yardımcı olur musunuz? 

135 Görüntülenme

3 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-61684-1501749567.jpg
oğuzhanyiğit
18.08.2017 - 04:07
public class WriteInternal {
    public static void write(String fileName, String[] data, Context context){
        File myInternalFile = new File(context.getFilesDir(), fileName);

        if (myInternalFile.exists()) {
            // dosya varsa
        } else {
            // String dizisindeki tüm stringleri tek bir output haline çevir
            String output = "";
            for (String item: data) {
                output += item + "\n";
            }
            dosyayaYaz(output,myInternalFile);
        }
    }
    
    public static void dosyayaYaz(String data, File fileName ) {
        FileWriter fw = null;
        BufferedWriter bw = null;
        PrintWriter pw = null;
        try {
            fileName.createNewFile();
            fw = new FileWriter(data);
            bw = new BufferedWriter(fw);
            pw = new PrintWriter(bw);

            pw.write(data);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pw != null) {
                    pw.close();
                }

                if (bw != null) {
                    bw.close();
                }

                if(fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Sana tavsiyem yaptığın methodu statik yapman, böylelikle istediğin heryerden çağırabilirsin

WriteInternal.write(......);

Çağrı Uğurel
18.08.2017 - 04:26
Tamam çok teşekkür ederim. Aslında program oldu ama bir kaç eksik yer var oraları halledersem eser çıkacak. :D Çok teşekkür ederim. :)
Oğuzhan Yiğit
18.08.2017 - 04:36
Hallolmasına sevindim kolay gelsin tekrardan
Çağrı Uğurel
18.08.2017 - 05:00
Hocam yine hallolmadı denedim fakat iş daha garip oldu. Bu sefer veri eklemiyor. :/ Şuan şirketteyim. Eve döneyim sana ulaşayım. :)
picture-6840-1502828261.jpg
cugurel7
18.08.2017 - 03:55
public class WriteInternal {
    public void WriteInternal(String fileName, String[] data, Context context){

        try {

            File myInternalFile = new File(context.getFilesDir(), fileName);

            if (myInternalFile.exists()) {
                // dosya varsa
            } else {
                // String dizisindeki tüm stringleri tek bir output haline çevir
                String output = "";
                for (String item: data) {
                    output += item + "\n";
                }
                dosyayaYaz(output,myInternalFile);
            }}}
        
            public static void dosyayaYaz(String data, File fileName ) {
            FileWriter fw = null;
            BufferedWriter bw = null;
            PrintWriter pw = null;
            try {
                fileName.createNewFile();
                fw = new FileWriter(data);
                bw = new BufferedWriter(fw);
                pw = new PrintWriter(bw);

                pw.write(data);

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (pw != null) {
                        pw.close();
                    }

                    if (bw != null) {
                        bw.close();
                    }

                    if(fw != null) {
                        fw.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}

dosyayazın altında cath or finally expected hatası veriyor olayı tam çözemedim :/

picture-61684-1501749567.jpg
oğuzhanyiğit
18.08.2017 - 03:06
  File myInternalFile = new File(context.getFilesDir(), fileName);

  if (file.exists()) {
     // dosya varsa
  } else {
      // String dizisindeki tüm stringleri tek bir output haline çevir
      String output = "";
      for (String item: data) {
         output += item + "\n";
      }
      dosyayaYaz(output,myInternalFile);
  }

   public static void dosyayaYaz(String yazilacakVeri, File yazilacakDosya) {
        FileWriter fw = null;
        BufferedWriter bw = null;
        PrintWriter pw = null;
        try {
            yazilacakDosya.createNewFile();
            fw = new FileWriter(yazilacakDosya);
            bw = new BufferedWriter(fw);
            pw = new PrintWriter(bw);

            pw.write(yazilacakVeri);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pw != null) {
                    pw.close();
                }

                if (bw != null) {
                    bw.close();
                }

                if(fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Kolay gelsin

Çağrı Uğurel
18.08.2017 - 03:45
Öncelikle teşekkür ederim. Benim kullandığım farklı bir yazma dosyası var. Onun yerine bunu mu kullanmamı tavsiye edersiniz yoksa sorunda kopyaladığım koda alt alta yazmamı sağlayabilecek bir kod var mı? :)
Oğuzhan Yiğit
18.08.2017 - 04:12
Yeni attığım class'ı rahatalıkla kullanabilirsin