我经常看到的样式是这样的:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test {
static public void main(String args[]) throws Exception {
FileInputStream fin = new FileInputStream("infile.txt");
FileOutputStream fout = new FileOutputStream("outfile.txt");
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
while (true) {
int ret = inc.read(bb);
if (ret == -1)
break;
bb.flip();
outc.write(bb);
bb.clear();
}
}
}
但我更喜欢这种风格,其中每个元素都从下一行开始:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test
{
static public void main(String args[]) throws Exception
{
FileInputStream fin = new FileInputStream("infile.txt");
FileOutputStream fout = new FileOutputStream("outfile.txt");
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
while (true)
{
int ret = inc.read(bb);
if (ret == -1)
break;
bb.flip();
outc.write(bb);
bb.clear();
}
}
}
我认为这种写法更易于阅读,但如果我使用这种方式会不会在与他人合作时遇到问题?