l**********o 发帖数: 260 | 1 新手初到,写了几行,请大家指点
// this is to produce an integer array with one number duplicated
import java.util.*;
import java.io.*;
public class proAnArray{
public static void main(String[] args) throws IOException{
FileWriter fw = new FileWriter("Array.txt");
int N = 30000;
fw.write(N + "\n");
for(int i = 0; i < N; i++){
fw.write(i + " ");
if (i % 10 == 0) fw.write("\n");
}
int j = 400;
fw.write(j + "\n"); ... 阅读全帖 |
|
q***s 发帖数: 2243 | 2 比如下面的代码,操作完成之后,是只关闭 FileWriter,还是只关闭 BufferedWriter
,还是两个都要关闭,如果两个都关闭,那么谁先关,谁后关?
FileWriter fw = new FileWriter("c:/tmp/test.txt");
BufferedWriter bw = new BufferedWriter(fw);
// some operations
bw.close();
fw.close();
多谢! |
|
J*****a 发帖数: 4262 | 3 这是linkedin第一轮电面的一位国人大哥给我的“warm-up”题,首先是序列化
我写成了tree类的成员函数,所以没有把root当成方法的参数
public void serialize(){
File file = new File("test.txt");
if(root == null) return;
FileWriter fw = null;
try{
fw = new FileWriter(file);
BufferedWriter bf = new BufferedWriter(fw);
Queue q = new LinkedList();
q.add(root);
while(!q.isEmpty()){
Node cur = q.poll();
if(cur == null) bf.write("# ");
else{
bf.write(cur.data + " ");
... 阅读全帖 |
|
m********0 发帖数: 2717 | 4 送你一个EA,(copyright "Andrew Whaley")
专门从MT下载intraday data的。
别搞这么恶心的标题了。
//+---------------------------+
//| Historic Data Dumping EA |
//+---------------------------+
#property copyright "Andrew Whaley"
extern int min_year = 2010;
extern int max_year = 2012;
// Global scope
int handle;
int init()
{
int p = Period();
string pd;
if (p == 1) pd = "M1";
else if (p == 5) pd = "M5";
else if (p == 15) pd = "M15";
else if (p == 30) pd = "M30";
else if (p == 60) pd = "H1";
else if ... 阅读全帖 |
|
I***e 发帖数: 1136 | 5 I have a few classes accessing a "data.txt" file. I want to create a .jar file
and package everything in. However, how do I point to the data.txt file
inside the jar?
If it is outside I can easily use something like
FileWriter fw=new FileWriter("C:\\data.txt");
Now what should I do?
Thanks.
-iCare- |
|
u****s 发帖数: 2186 | 6 A.
PrintWriter out = new PrintWriter(new FileWriter("test.txt"));
out.println("Hello World!");
out.close();
B.
BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"));
bw.write("Hello World!\n");
bw.close();
C.
DataOutputStream dos = new DataOutputStream(new FileOutputStream("
test.txt"));
dos.writeChars("Hello World!\n");
dos.close();
Which one or ones above will write a line of plain text Hello World! into a
tex |
|
y****e 发帖数: 1012 | 7 ava.io.FileNotFoundException: cache.csv (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(FileOutputStream.java:212)
at java.io.FileWriter.(FileWriter.java:107)
at edu.uiuc.cs.datacenterplacement.location.DataCache.addValue(
DataCache.java:98)
at edu.uiuc.cs.datacenterplacement.location.CoordinatesManager.
searchLocationPostalCode(CoordinatesManager.java:73)
at sun.reflect.GeneratedMethodAc... 阅读全帖 |
|
y****e 发帖数: 1012 | 8 cache.csv was created in class DataCache.
public class DataCache {
private static final String FILENAME = "cache.csv";
private static final double TOLERANCE = 0.1;
private static List memCacheCoord = new ArrayList<
Coordinate>()
;
private static List memCacheConte = new ArrayList();
.....
public static void addValue(double lat, double lng, Object data) {
try {
DecimalFormat format = new De... 阅读全帖 |
|
n******1 发帖数: 3756 | 9 我在网上看到这两段代码,一个写,一个读,但是好像是有问题的.我看写是没问题的
,我把queue打出来,输入的都有,但是reader的读行为非常奇怪,如果文件有内容,
可以读出来,但是重新写入的有时候能读一部分,比如writer写入abcde,可能read到ab
出来,但大部分时间都是null,尝试自己加过在writer加入sleep,wait什么都没用
Writer.java
import java.io.BufferedWriter;
import java.io.Console;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.logging... 阅读全帖 |
|
n******1 发帖数: 3756 | 10 我知道了,一个比较低级的错误,
w = new PrintWriter(new BufferedWriter(new FileWriter(filename,true)), true
);
这里FileWriter需要加set append = true, 原来的true只是让PrintWriter auto
flush
我开始以为filereader是不是读的太快了,就算遇到空行也会往下读,实际上应该不会
,如果在print之前判断是否null,这样程序就演示的挺好了
谢谢了 |
|
t********k 发帖数: 808 | 11 I want to read or write a binary file in pb5.
But the functions,such as FileRead,FileWrite are
work in ASCII.So i want to use API functions.
OpenFile or CreateFile
ReadFile or ReadFileEx
WriteFile or WriteFileEx
CloseHandle
Can you tell how to declare these extern functions
in pb?
BTW,do we can use following functions in pb?
How to declare them?
_lopen,_lread,_lwrite,_lclose
Thanks. |
|
t********k 发帖数: 808 | 12 I want to read or write a binary file in pb5.
But the functions,such as FileRead,FileWrite are
work in ASCII.So i want to use API functions.
OpenFile or CreateFile
ReadFile or ReadFileEx
WriteFile or WriteFileEx
CloseHandle
Can you tell how to declare these extern functions
in pb? |
|
z****u 发帖数: 15 | 13
You are not using the right classes. In JDK1.1 and later,
use
PrintWriter dos = new PrintWriter( new
FileWriter("file.txt"));
dos.println("a string here");
why do you think it is NOT simple?
In C:
FILE dos = fopen("file.txt", "w");
fprint(dos, "a string here\n");
In C++
ofstream dos = new ofstream("file.txt");
dos << "a string here" << endl;
same logic, same approach |
|
m**c 发帖数: 90 | 14
String str20 = ...;
Writer writer = new BufferedWriter(new FileWriter(new File(...)));
write.write (str20, 0, 5); // cannot remember it should 4 or 5
writer.close(); |
|
st 发帖数: 1685 | 15
~~~~~~~~no need to
nest one more bah.
BufferedWriter w=new BufferedWriter(new FileWriter("filename")); should work. |
|
g******i 发帖数: 32 | 16 我用的是 FileWriter 的writer, 请问如何向文件里写一个换行符?
还有写double的时候,写的是科学符点,即1.2E-4,如何才能写小数呢?我用的是writer
(Double.toString()). |
|
I*******e 发帖数: 1879 | 17 ☆─────────────────────────────────────☆
bee (只会嗡嗡叫的蜜蜂) 于 (Thu Jan 16 08:13:40 2003) 提到:
Currently I use FileWriter to open a file in append mode.
If meet exception, then return "file is locked", and closed the file if
file is not locked.
Is there any other way? Just curious. Thanks.
☆─────────────────────────────────────☆
magicfat (魔法胖子) 于 (Thu Jan 16 10:42:48 2003) 提到:
If you are using JDK 1.4, java.nio.channels.FileChannel.lock() or trylock()
is probably what you are looking for.
I |
|
A**o 发帖数: 1550 | 18 BufferedWriter out = new BufferedWriter(new FileWriter("
outPutFileName", true)); |
|
y***d 发帖数: 2330 | 19 我觉得 80 列太窄了;现在的那些函数、变量名很多就是词组,什么“
RandomAccessFile”,什么“BufferedReader”;再加上 class-function-try-for 之
类的缩进,80 列造成很多不必要的换行。
比如 http://download.oracle.com/javase/tutorial/essential/io/charstreams.html,
try {
inputStream =
new BufferedReader(new FileReader("xanadu.txt"));
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));
这不是折腾自己么;新时代应该以 120 列为标准了 |
|