Today, I read the Basic I/O in the Java tutorial and I find some problem:
public class CopyCharacters {
public static void main(String[] args) throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("/workspaces/test/a.txt");
outputStream = new FileWriter("/workspaces/test/b.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
But when I run the demo, it failed. In the Console:
Exception in thread "main" java.io.FileNotFoundException: /workspaces/test/b.txt (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
at java.io.FileWriter.<init>(FileWriter.java:63)
at Demo.CopyCharacters.main(CopyCharacters.java:13)
How can I do that?
File might have a lock and forbid you to open it for writing (i.e. your application is still on a break point in debug mode and you forgot to stop it or you killed the app and the process is still running in memory). You can check by doing this:
inputStream = new FileReader("/workspaces/test/a.txt");
File outFile = new File("/workspaces/test/b.txt");
if (!outFile.canWrite()) {
System.err.println("Cannot write into file: " + outFile.getAbsolutePath());
}
outputStream = new FileWriter(outFile);
You can also renname your out file "b.txt" for something else and it will work as before (until you locked it again by accident). An other way to do this is to use a temporary file:
public static void main(String[] args) throws Exception {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("/workspaces/test/a.txt");
File file = File.createTempFile("test", null);
outputStream = new FileWriter(file);
System.out.println(file.getAbsolutePath());
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
Good for coding (and debugging). It ensures that it will be deleted by the OS after.
Maybe you should try to use a new feature that takes care of your resources automatically by putting them inside the try-catch block?
public static void main(String[] args) throws IOException {
try (
FileReader inputStream = new FileReader("/workspaces/test/a.txt");
FileWriter outputStream = new FileWriter("/workspaces/test/b.txt");
)
{
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
}
}
If you write your code in this manner, you don't need the finally block, because java will take care of the resources that are inside the try block brackets.
Related
I ran over some problem with PrintWriter. I wrote some code that simply takes some input from a file and outputs it to another one.
Though a file is created, the file remains empty. The wanted input can be easily printed out in the console, which means the FileInputStream is working correctly.
Why is PrintWriter not printing anything?
public static void writeInFile(File in, File out) throws FileNotFoundException {
PrintWriter outputStream = null
Scanner scanner = new Scanner(new FileInputStream(in));
outputStream = new PrintWriter(new FileOutputStream(out));
outputStream.print("test");
while(scanner.hasNext()) {
outputStream.print(scanner.nextLine() + "\n");
}
scanner.close();
}
Make sure you always close your OutputStreams:
while(scanner.hasNext()) {
String s = scanner.nextLine();
outputStream.print(s+"\n");
System.out.println("Test "+s); //d.h. das Problem liegt am outputstream
}
outputStream.close();
scanner.close();
Edit: When you close the outputStream it calls flush automatically, which writes the buffer to the file. Without closing it the buffer may never be emptied/written to the file, as was the case here.
Also see this answer.
When dealing with IO which requires cleanup, I prefer to use auto resource cleanup. This is all you need at the most basic level:
public static void writeInToOut(InputStream in, OutputStream out) {
try(PrintWriter outputStream = new PrintWriter(out);
Scanner scanner = new Scanner(in)) {
while(scanner.hasNext()) {
outputStream.print(scanner.nextLine()+"\n");
}
}
}
You can now overload this function in several ways:
public static void writeInToOut(File file, OutputStream out) {
try (InputStream in = new FileInputStream(file)) {
writeInToOut(in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}
public static void writeInToOut(File inFile, File outFile) {
try (InputStream in = new FileInputStream(inFile);
OutputStream out = new FileOutputStream(outFile)) {
writeInToOut(in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}
public static void writeStdInToFile(File file) {
try (OutputStream out = new FileOutputStream(file)) {
writeInToOut(System.in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}
I JCIFS using the API to perform the reading of a text file that is in my local network, the code below I can open the file;
Code:
String sharedFolder ="tratermik";
String path="smb://192.168.43.80/"+sharedFolder+"balanca.txt";
SmbFile smbFile = new SmbFile(path);
How do I copy this file to my internal memory ?
The code below solved my problem.
I hope I can help somebody else
private static final byte[] buffer = new byte[60416];
public void copiar(String serverPath, String localPath)
throws Exception {
SmbFile serverFile = new SmbFile("smb://192.168.43.80/tratermik/balanca.txt");
File localFile = new File("sdcard/sistemas/tratermic/balanca.txt);
InputStream in = new SmbFileInputStream(serverFile);
OutputStream out = new FileOutputStream(localFile);
try {
while (true) {
synchronized (buffer) {
int amountRead = in.read(buffer);
if (amountRead == -1) {
break;
}
out.write(buffer, 0, amountRead);
}
}
} catch (Exception e) {
throw e;
} finally {
if (in != null) { in.close();}
if (out != null) {out.close();}
}
}
}
I'm executing an ANT task using Java code, via an executable jar. I'd like to include the build.xml within the executable JAR, but can't figure out how to reference it in my code. Any help is appreciated.
public static void main(String[] args) {
BuildLogger logger = new DefaultLogger();
logger.setMessageOutputLevel(Project.MSG_INFO);
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.out);
logger.setEmacsMode(true);
ProjectHelper ph = ProjectHelper.getProjectHelper();
Project p = new Project();
p.addBuildListener(logger);
p.init();
p.addReference("ant.projectHelper", ph);
//File f = new File(this.getClass().getResource("/report.xml").toURI()); I can't do toURI on this, it throws an exception
ph.parse(p, this.getClass().getResource("/report.xml")); //This throws a NullPointerException within ANT
p.executeTarget("dumpandreport");
}
If I create a java.io.File object that references an external build.xml file and specify that in ph.parse, this works...If I try to reference a file that is packaged within the JAR, this doesn't. I have validated (via 7-ZIP) that the file report.xml is, in fact, in the root of the JAR.
Well, disappointingly, I never did quite figure this out. You can, however, do the following:
public static void main(String[] args) {
...
ph.parse(p, getAntXML()};
...
}
private Object getAntXML() throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = this.getClass().getResourceAsStream("/report.xml");
File f = File.createTempFile("report", "xml");
outputStream = new FileOutputStream(f);
int read;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
return f;
} catch (IOException ex) {
throw ex;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
//Nop
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
//Nop
}
}
}
}
This works well enough for my purposes anyway.
There's something going on here that I don't understand. This code deletes all the files in the "stuff"directory:
public static void main(String[] args) throws Exception {
File dire = new File("C:\\Users\\spacitron\\Desktop\\Stuff");
for (File doc : dire.listFiles()) {
doc.delete();
}
}
However it won't work if I try to do something useful with it, such as only deleting duplicate files:
public static void main(String[] args) throws Exception {
File dire = new File("C:\\Users\\spacitron\\Desktop\\Stuff");
ArrayList<String> hashes = new ArrayList<>();
for (File doc : dire.listFiles()) {
String docHash = getHash(doc);
if (hashes.contains(docHash)) {
doc.delete();
} else {
hashes.add(docHash);
}
}
}
public static String getHash(File d) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA1");
FileInputStream inStream = new FileInputStream(d);
DigestInputStream dis = new DigestInputStream(inStream, md);
BufferedInputStream bis = new BufferedInputStream(dis);
while (true) {
int b = bis.read();
if (b == -1)
break;
}
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
BigInteger bi = new BigInteger(md.digest());
return bi.toString(16);
}
What gives?
You need to close your input streams in a finally block would be best, These will be accessing you files still preventing them from being deleted as they are in use
FileInputStream inStream = null;
DigestInputStream dis = null;
BufferedInputStream bis = null;
try {
md = MessageDigest.getInstance("SHA1");
inStream = new FileInputStream(d);
dis = new DigestInputStream(inStream, md);
bis = new BufferedInputStream(dis);
while (true) {
int b = bis.read();
if (b == -1)
break;
}
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
} finally {
try{
if(inStream!= null)
inStream.close();
if(dis != null)
dis.close();
if(bis != null)
bis.close()
} catch (Exception ex){
ex.printStackTrace()
}
}
Windows does not permit deleting files that are open, unless they are opened with a special flag that is unavailable when programming in Java. While this code would work on a Unix system, on Windows it won't.
Closing open files is a good idea in general because operating systems impose a limit on the number of files that an application can have open at any given time.
I have a simple file read and write function.
private void WriteToFile(String filename, String val) {
PrintWriter outStream = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filename);
outStream = new PrintWriter(new OutputStreamWriter(fos));
outStream.print(val);
outStream.close();
} catch (Exception e) {
if (outStream != null) {
outStream.close();
}
}
}
private String ReadFile(String filename) {
String output = "";
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(filename);
br = new BufferedReader(fr);
output = br.readLine();
br.close();
} catch (Exception e) {
if (br != null) {
br.close();
}
}
return output;
}
When building I get:
unreported exception java.io.IOException; must be caught or declared to be thrown
br.close();
^
Why do I need to catch br.close but it doesn't complain about WriteToFile's close()?
Taken from the source code of java.io.PrintWriter:
public void close() {
try {
synchronized (lock) {
if (out == null)
return;
out.close();
out = null;
}
}
catch (IOException x) {
trouble = true;
}
}
The IOException was eaten up within the close() method in PrintWriter
From source code of java.io.BufferedReader:
public void close() throws IOException {
synchronized (lock) {
if (in == null)
return;
in.close();
in = null;
cb = null;
}
}
BufferedReader throws the IOException.
That should answer your question.
Why do I need to catch br.close but it doesn't complain about WriteToFile's close()?
You can check the Java Docs for this. The close() method for BufferedReader :
public void close()
throws IOException
And the close() method for PrintWriter :
public void close()
That answer's your question as to why JVM doesn't complain. Because it is clear from the method signatures;
PrinterWriter.close() doesn't throw any Exception.
If you call fos.close(), it will ask you to catch/throw the exception.
In the PrintWriter.java. The exception is caught and handled. So you needn't catch it while using.
Java Source:
public void close() {
try {
synchronized (lock) {
if (out == null)
return;
out.close();
out = null;
}
}
catch (IOException x) {
trouble = true;
}
}
But in BufferedReader the exception is thrown. So you have to catch it when using.
Java Source:
public void close() throws IOException {
synchronized (lock) {
if (in == null)
return;
in.close();
in = null;
cb = null;
}
}