JAVA จะเขียนยังไงให้ อ่านข้อมูล แต่ละบรรทัด ลงแยก array กันคะ

เริ่มโดย nuugib, 17 พฤษภาคม 2012, 15:30:39

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

nuugib

คือตอนนี้เขียนให้อ่านข้อมูลจาก text file ขึ้นมาแบบ stream

ทีนี้ถ้าต้องการให้แบบว่า บรรทัดที่ 1 อยู่ที่ array แรก  , บรรทัดที่ 2 อยู่ที่ array ต่อไป

วนอ่านจนหมด ทุกบรรทัด

ควรเขียนยังไงดีคะ

ขอบคุณล่วงหน้าค่ะ

(คือเราอ่อนจาวามากๆ เพิ่งมาเขียนใหม่ปีนี้คะ)

iLhay

Arrays มีทั้งหมดกี่ตัวอะครับ
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting

[/direct]
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting[/direct] เริ่มต้นปีละ 790 บาท NVMe SSD เร็ว 9000MB/s เร็วกว่านี้ไม่มีอีกแล้ว
[direct=https://bangmod.cloud/cloud-server]Cloud Server[/direct] เริ่มต้นเพียงเดือนละ 159 บาท พร้อมใช้ภายใน 1 นาที ผ่านระบบอัตโนมัติมีทั้ง Linux / Windows / DirectAdmin
สอบถามข้อมูลและแจ้งปัญหา 02-105-4417 ตลอด 24 ชั่วโมง

nuugib

อ้างถึงจาก: iLhay ใน 17 พฤษภาคม 2012, 15:35:31
Arrays มีทั้งหมดกี่ตัวอะครับ

เป็น array 1 มิติ อะคะ


แบบว่าเช่น

textfile  ชื่อ  AAA.txt

ข้างในมีข้อมูล

Content Row 1
Content Row 2

แค่สองบรรทัดนี้ค่ะ


อยากให้อ่านไฟล์ AAA.txt

แล้วจับหยอด

ตัวแปร1 = Content Row 1

ตัวแปร2 = Content Row 2


แบบนี้อะคะ

แ่ต่ว่าไม่ถนัดจาวาเอาซะเลย เลยมึนติ้บๆๆ

iLhay

ทราบแล้วครับว่าเป็น Sing Array

แต่ว่าถ้าสมมุติผมมีข้อมูล 10 ตัว

ก็จะมี Single Arrays ทั้งหมด 10 ตัว ?
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting

[/direct]
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting[/direct] เริ่มต้นปีละ 790 บาท NVMe SSD เร็ว 9000MB/s เร็วกว่านี้ไม่มีอีกแล้ว
[direct=https://bangmod.cloud/cloud-server]Cloud Server[/direct] เริ่มต้นเพียงเดือนละ 159 บาท พร้อมใช้ภายใน 1 นาที ผ่านระบบอัตโนมัติมีทั้ง Linux / Windows / DirectAdmin
สอบถามข้อมูลและแจ้งปัญหา 02-105-4417 ตลอด 24 ชั่วโมง

nuugib

อ้างถึงจาก: iLhay ใน 17 พฤษภาคม 2012, 15:39:31
ทราบแล้วครับว่าเป็น Sing Array

แต่ว่าถ้าสมมุติผมมีข้อมูล 10 ตัว

ก็จะมี Single Arrays ทั้งหมด 10 ตัว ?

ค่ะ

แต่ที่จะใช้จริง จะมีข้อมูล แค่ 2 บรรทัด ค่ะ

แนะนำหน่อยค่ะ ขอบคุณมากๆค่ะ

iLhay

ก็อ่านมาแล้ววนเข้าตาม for คับ
for ( i = บลาบลาสบลา ){
if ( i == 1 )
   ตัวแปร2 = ;
}
เสร็จละคับ
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting

[/direct]
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting[/direct] เริ่มต้นปีละ 790 บาท NVMe SSD เร็ว 9000MB/s เร็วกว่านี้ไม่มีอีกแล้ว
[direct=https://bangmod.cloud/cloud-server]Cloud Server[/direct] เริ่มต้นเพียงเดือนละ 159 บาท พร้อมใช้ภายใน 1 นาที ผ่านระบบอัตโนมัติมีทั้ง Linux / Windows / DirectAdmin
สอบถามข้อมูลและแจ้งปัญหา 02-105-4417 ตลอด 24 ชั่วโมง

sirisak



import java.util.*;
import java.io.*;
import java.text.*;
public class  TextRead
{
   public static void main(String[] args)
   {
      File file = new File("file.txt");
      String[] array = new String[10];
      String temp;     
      try {
         BufferedReader br = new BufferedReader(new FileReader(file));       


int index = 0;
  while( (line = br.readLine()) != null) {
        array[index++] = line;
}

         br.close();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

// ลองพิมพ์ข้อมูลออกจาก Array
for (int x=0;x<=10;x++)
  {
 
  System.out.println("Data Of  Array "+x+ " = "+array[x]); 

  }

   }
}




ลองรันดูครับ มั่วๆไป อิอิ อันนี้กรณีมี 10 บรรทัดครับ ลองมั่วๆดูครับ

yosima

ไม่ได้โพสนาน เห็นคนถามเรื่องโปรแกรมแล้วอดไม่ได้
อันนี้ผมเอาลง ArrayList แทน Array นะครับ วิธีใช้ก็เหมือนกันเลย ดูตรง for loop เอา
อ้างถึงimport java.io.*;
import java.util.ArrayList;
import java.util.List;
public class test {
    public static void main(String args[])
     {
        try{
           FileInputStream fstream = new FileInputStream("C:\\test.txt");
           DataInputStream in = new DataInputStream(fstream);
           BufferedReader br = new BufferedReader(new InputStreamReader(in));
           String strLine;
           List list = new ArrayList();
           while ((strLine = br.readLine()) != null) {
              list.add(strLine);
           }
           for (int i=0;i<list.size();i++){
              System.out.print(list.get(i));
           }
           in.close();
        }catch (Exception e){
           System.err.println("Error: " + e.getMessage());
        }
     }
}

iLhay

น่านแหละตามนี้เลยครับ

ผมอยากจะใบ้มากกว่าการทำให้ไปเปล่าๆ -"-
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting

[/direct]
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting[/direct] เริ่มต้นปีละ 790 บาท NVMe SSD เร็ว 9000MB/s เร็วกว่านี้ไม่มีอีกแล้ว
[direct=https://bangmod.cloud/cloud-server]Cloud Server[/direct] เริ่มต้นเพียงเดือนละ 159 บาท พร้อมใช้ภายใน 1 นาที ผ่านระบบอัตโนมัติมีทั้ง Linux / Windows / DirectAdmin
สอบถามข้อมูลและแจ้งปัญหา 02-105-4417 ตลอด 24 ชั่วโมง

nuugib