خلاصه مطلب
در این مثال یک آرایه را در فایل ذخیره می کنیم. بعد آرایه را از فایل میخوانیم ، بعضی از عناصر درون آرایه را دستکاری کرده و دوباره در فایل ذخیره می کنیم.
نوع محتوامقاله آموزشی
موضوعآموزش برنامه نویسی »
زمان مطالعه۱ دقیقه مطالعه
آخرین ویرایش۱۸ خرداد ۱۳۹۶
جاوا
.png)
مثال کار با فایل در جاوا
.png)
مثال کار با فایل در جاوا 
آموزش آسان و ساده زبان برنامه نویسی جاوا لینک دانلودعضو کانال تلگرام
| package javalike; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class ExampleArrayFile { public ExampleArrayFile() { } public static int[] read() { String filename = "array.txt"; String strArray = ""; String line; try { BufferedReader in = new BufferedReader(new FileReader(filename)); while ((line = in.readLine()) != null) { strArray += line + " "; } in.close(); } catch (IOException e) { e.printStackTrace(); } String[] temp = strArray.split(" "); int array[] = new int[temp.length]; for (int i = 0; i < array.length; i++) { array[i] = Integer.parseInt(temp[i]); } return array; } public static void write(int[] x) { String filename = "array.txt"; BufferedWriter outputWriter = null; try { outputWriter = new BufferedWriter(new FileWriter(filename)); for (int i = 0; i < x.length; i++) { outputWriter.write(x[i] + ""); outputWriter.newLine(); } outputWriter.flush(); outputWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { int a[] = { 1, 5, 10, 123, 44, 88, 99, 147 }; // write an array to a file write(a); // Reading array in file int b[] = read(); System.out.println(b[5]); b[5] += 10; b[2] = 6; b[7] = 1000; // write an array to a file write(b); } } |
.png)
| int a[] = { 1, 5, 10, 123, 44, 88, 99, 147 }; // write an array to a file write(a); // Reading array in file int b[] = read(); System.out.println(b[5]); b[5] += 10; b[2] = 6; b[7] = 1000; // write an array to a file write(b); |
| 88 |
.png)


