标签: file-io

未从文件中正确读取值

我试图从用逗号分隔的文件中读取x,y坐标.但是,元素未正确添加到ArrayList.我在哪里错了?

ArrayList<Double> xpointArrayList = new ArrayList<Double>();
ArrayList<Double> ypointArrayList = new ArrayList<Double>();
try {
    BufferedReader input = new BufferedReader(new FileReader(args[0]));
    String line;
    while ((line = input.readLine()) != null) {
        line = input.readLine();
        String[] splitLine = line.split(",");

        double xValue = Double.parseDouble(splitLine[0]);
        double yValue = Double.parseDouble(splitLine[1]);

        xpointArrayList.add(xValue);
        ypointArrayList.add(yValue);
    }
    input.close();

    } catch (IOException e) {

    } catch (NullPointerException npe) {

    }

    double[] xpoints = new double[xpointArrayList.size()];
    for (int i = 0; i < xpoints.length; i++) {
        xpoints[i] = xpointArrayList.get(i);
    }
    double[] ypoints = …
Run Code Online (Sandbox Code Playgroud)

java arrays file-io arraylist

0
推荐指数
1
解决办法
73
查看次数

如何在内置的open()函数中输入文件路径?

我已经阅读了文档,但它没有告诉我们如何在open()函数中输入文件路径.

如果文件路径是:

/opt/myapp/report/sample.txt 
Run Code Online (Sandbox Code Playgroud)

要么

$MYPATH/report/sample.txt (其中$ MYPATH =/opt/myapp)

以这种方式编写语句是否可以:

f = open('/opt/myapp/report/sample.txt', "r")
Run Code Online (Sandbox Code Playgroud)

要么

f = open('$MYPATH/report/sample.txt', "r")
Run Code Online (Sandbox Code Playgroud)

python file-io input

0
推荐指数
1
解决办法
641
查看次数

Java按日期保存对象

我试图将对象保存到下面的类的文件,但它给了我java.io.NotSerializableException.

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Calendar;

class Saver {

  Calendar cal = Calendar.getInstance();

  public void save(ArrayList<Product> products) {
    for (int i = 0; i < products.size(); i++) {
      try { 
        FileOutputStream saveFile = new FileOutputStream(
        "/" + products.get(i) + ".product"
        );

        ObjectOutputStream oos = new ObjectOutputStream(saveFile);
        oos.writeObject(products.get(i));
      } catch(Exception ex) {
        System.out.println(ex); 
      }
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

而产品类看起来像这样:

class Product {

  private String title;
  private int id;
  private double price;

  public Product(String title, int id, double price) { …
Run Code Online (Sandbox Code Playgroud)

java io file-io exception object

0
推荐指数
1
解决办法
96
查看次数

在Octave中使用textscan()...如何正确格式化?

我在磁盘上有一个数据集,正在逐行读取,我想将数据列之一转换为浮点向量(范围为0-23.99999)(当天)。

数据如下所示:

2010/01/01,00:00:00.979131, 27.4485,  51.9362, 14.8,  6
2010/01/01,00:00:01.021977, 27.5149,  51.9375, 16.0,  6
2010/01/01,00:00:01.074032, 27.4797,  51.9446, 14.5, 10
2010/01/01,00:00:01.663689, 25.8441,-152.8141, 14.6,  6
2010/01/01,00:00:01.639541, 25.8744,-152.6122,  1.5,  5
2010/01/01,00:00:02.232099, -2.2447,  11.5023, 18.8,  6
2010/01/01,00:00:02.256351, -0.8135,  27.3139, 17.7,  5
2010/01/01,00:00:02.306734, -2.7797,  28.5109, 26.0,  5
2010/01/01,00:00:02.620765, 25.6656,-154.2029, 26.2,  9
2010/01/01,00:00:02.658495, 25.6698,-154.2157, 23.0,  6
2010/01/01,00:00:02.731266, -5.7106, 126.4517,  3.6,  5
2010/01/01,00:00:02.787495, -5.7138, 126.5210, 24.4,  8
2010/01/01,00:00:02.811636, -3.2453, 124.6919, 21.1,  8
Run Code Online (Sandbox Code Playgroud)

第2栏(例如00:00:00.979131)很有趣,我想做类似的事情

setenv GNUTERM 'x11';
fid = fopen('myfile.txt', 'r');
m = textscan(fid, '%d%d%d%d/%d%d/%d%d, %d%d:%d%d:%d%f, %f, %f, %f, %d'); …
Run Code Online (Sandbox Code Playgroud)

formatting file-io numeric octave

0
推荐指数
1
解决办法
7537
查看次数

为什么File.exists返回误报

我的android File.exists功能有一个奇怪的问题.以下是我的代码

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_file_browser, container);

        DataBaseHelper tmpHelper = new DataBaseHelper(getActivity());
        ButtonInfoContainer infoContainer = tmpHelper.getVideoWithId(videoId);



        //Tmp way to use this. When UX is finished will create a better way.
        File f = new File(Environment.getExternalStorageDirectory() + "/aslkfj");
        Log.i(StaticValues.TAG, Environment.getExternalStorageDirectory() + "/aslkfj");

        LinearLayout tmpView2 = (LinearLayout) view.findViewById(R.id.custom_recordings);
        if(f.exists());
        {
            Log.i(StaticValues.TAG, "f exists");

            String[] tmp = f.list();
            View tmpView = null;

            if(tmp != null)
            {
                for(int i = 0; …
Run Code Online (Sandbox Code Playgroud)

java file-io android file

0
推荐指数
2
解决办法
512
查看次数

如何在文件中存储和检索对象的arraylist?

可能重复:
如何在java中序列化ArrayLIst而不会出现错误?

我在两个不同的班级有两个arraylists.一种是存储交易对象temporarily和其他permanent.我使用一种addAll方法将对象从temporaryarraylist 复制到permanent.然后将对象从permanentarraylist 保存到file.当我的程序重新启动时,permanentarraylists中的对象将从中恢复file.但我得到一个例外.我的代码有什么问题?

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList;

/****@author Haleemdeen*/public class FileImportExport {

private ArrayList<Stock> permntTransactions=new ArrayList<Stock>();


void cancatToPerrmntTransactions(ArrayList<Stock> arraylist1){
    permntTransactions.addAll(arraylist1);
}

ArrayList<Stock> displayPermntTransactions(){
    return permntTransactions;
}

    void exportToFile(){

    try{  // Catch errors in I/O if necessary.
    // Open a file to write to, named SavedObj.sav.
    FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");

    // Create an ObjectOutputStream to put …
Run Code Online (Sandbox Code Playgroud)

java file-io arraylist

0
推荐指数
1
解决办法
6553
查看次数

读写文件 - Java空格

我想从文件中读取并写入文件.输入文件如下

<ORLANDO>   <0%>
    As I remember, Adam, it was upon this fashion bequeathed me by will but poor a thousand crowns, and, as thou sayest,
<ORLANDO>

"A s   I   r e m e m b e r    A d a m    i t   w a s   u p o n   t h i s   f a s h i o n   b e q u e a t h e d   m e   b y   w i l l   b …
Run Code Online (Sandbox Code Playgroud)

java string file-io filewriter replaceall

0
推荐指数
1
解决办法
1679
查看次数

从文本文件填充JCombobox

我已经看到一个较旧的问题,答案是下面的代码,但如果我使用netbeans,我已经设计了我的comboBox.所以我认为(我想象的是Java和netbeans中的新东西!)代码的最后一行应该改变,我在哪里插入这段代码?

BufferedReader input = new BufferedReader(new FileReader(filePath));
List<String> strings = new ArrayList<String>();
try {
    String line = null;
    while (( line = input.readLine()) != null){
        strings.add(line);
    }
}

catch (FileNotFoundException e) {
    System.err.println("Error, file " + filePath + " didn't exist.");
}
finally {
    input.close();
}

String[] lineArray = strings.toArray(new String[]{});

JComboBox comboBox = new JComboBox(lineArray); 
Run Code Online (Sandbox Code Playgroud)

java file-io swing netbeans jcombobox

0
推荐指数
1
解决办法
2079
查看次数

C:fgetc给出了segfault

以下代码给了我一个段错误,我不知道为什么.除了最后返回0之外,它是我的主函数中唯一的代码.此外,它成功打印"fr open".

//create file pointer and open file
FILE *fr;
fr = fopen("IntegerArray.txt", "r");

if(fr = NULL){
        printf("fr is null\n");
}
else{
        printf("fr opened\n");
}

int ch = fgetc(fr);
ch = fgetc(fr);
Run Code Online (Sandbox Code Playgroud)

c file-io segmentation-fault

0
推荐指数
1
解决办法
1129
查看次数

用Java写入隐藏文件

我的Java隐藏文件有问题.我想写一个隐藏的txt文件,但它总是说"访问被拒绝".我的建议是让文件可见,写入文件然后再次隐藏.但是如何让隐藏文件可见?

java file-io hidden file

0
推荐指数
1
解决办法
2151
查看次数