+ -
当前位置:首页 → 问答吧 → java read file to array

java read file to array

时间:2014-02-19

来源:互联网

我想READ FILE DATA TO ARRAY,compile 无问题,但一RUN就出现以下画面:



咩野原因呢?另外我想将两个CLASS写埋入一个CLASS,即READFILE改用METHOD,应该点写呢?

THANKS 各位CHING指教。


// ReadFile.java
package javareadtextfile;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class ReadFile
{
public String[] readLines(String filename) throws IOException
{
FileReader fileReader = new FileReader(filename);

BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;

while ((line = bufferedReader.readLine()) != null)
{
lines.add(line);
}

bufferedReader.close();

return lines.toArray(new String[lines.size()]);
}
}



public class JavaReadTextFile
{
public static void main(String[] args)
{
ReadFile rf = new ReadFile();

// The text file location of your choice
String filename = "number.txt";

try
{
String[] lines = rf.readLines(filename);

for (String line : lines)
{
System.out.println(line);
}
}
catch(IOException e)
{
// Print out the exception that occurred
System.out.println("Unable to create "+filename+": "+e.getMessage());
}
}
}

[ 本帖最后由 I_love_IT 於 2014-1-17 03:12 PM 编辑 ]

作者: I_love_IT   发布时间: 2014-02-19

引用:原帖由 I_love_IT 於 2014-1-17 03:11 PM 发表
我想READ FILE DATA TO ARRAY,compile 无问题,但一RUN就出现以下画面:

3054552

咩野原因呢?另外我想将两个CLASS写埋入一个CLASS,即READFILE改用METHOD,应该点写呢?

THANKS 各位CHING指教。


// ...
comment out following line
package javareadtextfile;

[ 本帖最后由 form5 於 2014-1-17 08:49 PM 编辑 ]

作者: form5   发布时间: 2014-02-19

引用:原帖由 form5 於 2014-1-17 08:47 PM 发表


comment out following line
package javareadtextfile;
TAHNKS CHING

作者: I_love_IT   发布时间: 2014-02-19

Computer programs often need to read data that was prepared by a different program or even a different system.

This data is obtained from data storage devices such as hard disks, floppy disks, CD-ROM, magnetic tape and information servers on a network.

There are many generations of data storage devices an many kinds of computer systems that use them.


The specifics of each situation depend upon the technology of the device, the computer system, the language, the server and the network.

We will focus on a few principles and techniques that can be used immediately and which create a foundation for future work in this area.


IDL has tools that permit data to be read and written to files.

The details that depend on the hardware and operating system are largely hidden from the user.

We will concentrate on a subset of the tools that provide basic functionality.

Further information can be obtained by reading the section Building IDL Applications -- Files Input and Output found under the Contents tab in IDL Online Help.

_________


The basic steps in reading data from a file are:

Tell the program where to find the data.

Open a path to the data.

Set up the program variables to access the data

Read the data

Close the data path

作者: loko2014   发布时间: 2014-02-19

热门下载

更多