file-io

Java saving/opening File objects

跟風遠走 提交于 2020-01-03 08:21:05
问题 After painstakingly trying to implement TCP file transfer in my conference chat application using raw byte streams, I've decided it's much easier to send files that I want transferred through object streams. The files are sent and stored at their destination (whether that is the central server or the downloading client) as in-memory File objects. However, these files are no use as just File objects - clients should be able to open them. Is there a way in java to save File objects as hard-disk

Open file for read/write, create if needed

杀马特。学长 韩版系。学妹 提交于 2020-01-03 08:15:13
问题 What is the most elegant way to open a file such that the file gets created if it does not exist, the file won't be truncated if it does exist and it is possible to write any part of the file (after seeking), not just the end? As far as I can tell, the open builtin doesn't seem up to the task: it provides various modes, but every one I tried fails to satisfy at least one of my requirements: r+ fails if the file does not exist. w+ will truncate the file, losing any existing content. a+ will

What is an EOFError in Ruby file I/O?

徘徊边缘 提交于 2020-01-03 08:14:13
问题 The official documentation doesn't specify. I understand EOFError means "End of file error", but what exactly does that mean? If a file reader reaches the end of a file, that doesn't sound like an error to me. 回答1: EOFError is handy in all of IO, the class which is the basis of all input/output in ruby. Now also remember core Unix concepts: everything is a file. This includes sockets. So, if you have some socket open and are reading from it, an exceptional condition might be to encounter an

Reading strings into Matlab from excel?

[亡魂溺海] 提交于 2020-01-03 08:09:33
问题 I would like to read strings into Matlab from an excel file ID = xlsread('data.xlsx',1, 'D2:D4') the cells in range D2:D4 have strings in them. When I try to import the strings into Matlab all I get is an empty list? what can I do to fix this? 回答1: If you're in Matlab 2010 you can also do something like this to avoid having extra values in your workspace. [~, ~, raw] = xlsread('data.xlsx',1, 'D2:D4') 回答2: I need to use this [num, txt, raw] = xlsread('data.xlsx',1, 'D2:D4') the txt will import

Read from a file containing integers - java

≯℡__Kan透↙ 提交于 2020-01-03 07:05:08
问题 I am trying to read from a file that contains three numbers. The file looks like this: 45 20 32 My code is below: import java.awt.Color; import java.awt.Desktop.Action; import java.awt.GridLayout; import java.awt.Label; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; import

Accessing array contents inside .mat file in python

守給你的承諾、 提交于 2020-01-03 05:51:04
问题 I want to read a .mat file available at http://www.eigenvector.com/data/tablets/index.html. To access the data inside this file, I am trying the follwing: import scipy.io as spio import numpy as np import matplotlib.pyplot as plt mat = spio.loadmat('nir_shootout_2002.mat') # http://pyhogs.github.io/reading-mat-files.html def print_mat_nested(d, indent=0, nkeys=0): if nkeys>0: d = {k: d[k] for k in d.keys()[:nkeys]} if isinstance(d, dict): for key, value in d.iteritems(): print '\t' * indent +

Why File.exists() does not work

时间秒杀一切 提交于 2020-01-03 05:37:27
问题 I have a file watstheday.txt in my src/main/resources folder of my project as shown in the image file below. I read the file through a getResourceAsStream() method of the ClassLoader and perform further actions in my code which is working perfectly. However if I try to perform a check if the file exists through the below code it always returns false. try { ClassLoader classLoader = getClass().getClassLoader(); System.out.println("!@#!@# so difficult to be simple..."+classLoader.getResource(

How to Read 1st line of a file with BufferedReader?

谁说胖子不能爱 提交于 2020-01-03 04:48:07
问题 I was experimenting with BufferedReader to read 1st line file to a string. How do I do this? Also how can I read an entire file to a string? How to read a particular line like readline(int line) without iterating through the previous lines? File namefile = new File(root, ".name"); FileReader namereader = new FileReader(namefile); BufferedReader in = new BufferedReader(namereader); 回答1: You can use BufferedReader.readLine() to get the first line. Note that the next call to readLine() will get

JAVA I/O: Unexpected Performance Difference between Sequentially and Concurrently Reading Files using BufferedReader

我怕爱的太早我们不能终老 提交于 2020-01-03 04:33:35
问题 I basically trying to load 6 large file (500MB per file on average) using buffered reader. In one run, I loaded the files one by one; In the other run, I loaded the files in parallel. There appeared to be a significant difference in term of time consumed by the two runs: Sequential Run: 14.634s Parallel Run: 120.317s NOTE that this difference shows up clearly on the 1st time reading these files into the JVM. In subsequent reads, duration shortens significantly, which I assume is due to

read each line of a text up to the first space into a cell array in MATLAB

北慕城南 提交于 2020-01-03 03:19:06
问题 I have a multi line text file that in each line has 3 words separated by some spaces. I want to write the first word of each line into a nX1 cell array, so that: cell{1}{1}=line1_1stword cell{1}{2}=line2_1stword . . . How can I do that? I know that the following command reads each line into a line of the cell, but I want just the first word. fid = fopen(`myFile.ext`) allData = textscan(fid,'%s','Delimiter','\n'); 回答1: Try this - fid = fopen('myFile.ext') allData = textscan(fid,'%s','Delimiter