Java write to .csv file - java

I am trying to write to a .csv file, but I keep getting the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
void is an invalid type for the variable writeToFile
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
The error is associated with the line:
void writeToFile(String Filename){
Here is my code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileWriter;
public class writeFileExample {
public static void main(String[] args) {
void writeToFile(String Filename){
double steps=0;
File file=new File(Filename);
file.createNewFile();
FileWriter writer=new FileWriter(file);
try {
//Integrate integrate=new Integrate();
//for (steps=10;steps<1000000;steps=steps*10){
//double area_value=integrate.integrate_function(steps);
writer.write("Steps"+","+"Area");
//}
//System.out.println(area_value);
writer.flush();
writer.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
I can't see any syntax errors.
Taking into account Reimeus' comment below I edited it a bit. I now have:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileWriter;
void writeToFile(String Filename){
public class writeFileExample {
public static void main(String[] args) {
double steps=0;
etc.
I am getting the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Filename cannot be resolved to a variable
Any help appreciated.

Java doesnt support nested methods. Move writeToFile out of the main method

public class Test {
public static void main(String[] args) {
try {
writeToFile("c:\\abc.csv");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static public void writeToFile(String Filename) throws IOException
{
.
.
.
.
}
}

Related

What is wrong with my Java .html file generator

I am working on an .html file generator that can be used to view .swf files in a browser, however I'm getting a "Unresolved compilation problem" error. Is there possibly a problem with my imports?
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class CreateFile {
public static String starthtml = "<object><embed src=\"";
public static String endhtml = ".swf\" width=\"100%\" height=\"100%\"></embed></object>";
public static String s;
public static void main(String[] args) {
try {
File myObj = new File("Flash Loader.html");
if (myObj.createNewFile()) {
System.out.println("Flash Loader Created Successfully");
} else {
System.out.println("File already exists");
}
} catch (IOException e) {
System.out.println("An error occurred");
e.printStackTrace();
}
Scanner sc = new Scanner(System.in);
System.out.println("Enter SWF file name");
s = sc.nextLine();
try {
FileWriter myWriter = new FileWriter("Flash Loader.html");
myWriter.write(starthtml+sc+endhtml);
myWriter.close();
System.out.println("Successfully wrote to the file");
} catch (IOException e) {
System.out.println("An error occurred");
e.printStackTrace();
}
}
}
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at CreateFile.main(Flash_Loader_Creator.java:10)
Renaming my .java file to CreateFile.java fixed the issue thank you https://stackoverflow.com/users/1081110/dawood-says-reinstate-monica
https://stackoverflow.com/users/1902512/haibrayn-gonz%c3%a1lez

FileNotFound exception error even thought the file exists in eclipse

While running my java file io program I'm getting FileNotFoundException
I tried changing the directory of the file and most of the other solution mentioned in SO, nothing works.
My code:
package com.HelloWorld;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
FileWriter w=null;
BufferedWriter bw=null;
try {
String s="welcome";
String b="‪‪D:\\test.txt";
w=new FileWriter(b);
bw=new BufferedWriter(w);
bw.write(s);
bw.flush();
}
catch(IOException e)
{
System.out.println("exception caught"+e);
}
finally{
try {
if(bw!=null)
bw.close();}
catch(Exception e) {
System.out.println("exception caught"+e);
}
try {
if(w!=null)
{
w.close();
System.out.println("success");
}}
catch(Exception e) {
System.out.println("exception caught"+e);
}
}
}
}
I had already created the file in the D drive so the FileWriter overrides the already created file name because of which it did not write to the file

FileOutputStream doesn't show FileNotFoundException

for FileOutputStream, it will throw a FileNotFoundException if the file doesn't exist, but it will create it if it can.
I dont have a Sample.txt in my project root
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) {
try {
FileOutputStream s= new FileOutputStream("Sample.txt");
} catch (FileNotFoundException e) {
System.out.println("File not Found");
}
}
}
The problem is:
I cannot see the Output of the "File Not Found" from the Terminal. How did it happen?
Thank you
You can set Sample.txt as a File first and check if it exists with .canWrite()
You still have to put a try/catch around FileOutputStream, but it should never go in the catch block.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class test {
public static void main(String[] args) {
File f = new File("Sample.txt");
if (!f.exists()) {
System.out.println("File not Found");
}
else {
try {
FileOutputStream s = new FileOutputStream(f);
} catch (FileNotFoundException e) {}
}
}
}

Running xjc from java code

I am using xjc to generate classes from xsd. The generation has to happen inside the java code. Right now I have done it like this:
Process child = Runtime.getRuntime().exec(command);
try {
System.out.println("waiting...");
child.waitFor();
System.out.println("waiting ended..");
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
The output for the above program is:
waiting...
I have to use the classes after they are generated. The problem here is that the subprocess never exits and the control is never back to the java program!
Is there a way to do this without getRuntime().exec() ?
You can actually use the driver class (com.sun.tools.xjc.Driver) behind the command line tool. This worked for me:
import com.sun.tools.xjc.BadCommandLineException;
import com.sun.tools.xjc.Driver;
import com.sun.tools.xjc.XJCListener;
import org.xml.sax.SAXParseException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Generator {
public static void main(String[] args) throws BadCommandLineException, IOException {
final String targetDir = "jaxb-files";
Path path = Paths.get(targetDir);
if(!Files.exists(path)) {
Files.createDirectories(path);
}
Driver.run(new String[]{"-d", targetDir,
"D:\\dev\\onepoint\\tui\\java\\xsdjsonschema\\src\\main\\xsd\\test.xsd"}, new XJCListener() {
#Override
public void error(SAXParseException e) {
printError(e, "ERROR");
}
#Override
public void fatalError(SAXParseException e) {
printError(e, "FATAL");
}
#Override
public void warning(SAXParseException e) {
printError(e, "WARN");
}
#Override
public void info(SAXParseException e) {
printError(e, "INFO");
}
private void printError(SAXParseException e, String level) {
System.err.printf("%s: SAX Parse exception", level);
e.printStackTrace();
}
});
}
}
try this
Process child = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(
new InputStreamReader(child.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}

about classpath in run configuration in eclipse

Test1_Exec.java
import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("java -cp bin Test1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Test1.java:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args)
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Test1_Exec.class and Test1.class are both in the bin folder under JavaTest(project name), and the codes do work. But I want to replace the code "Process p = run.exec("java -cp bin Test1")" with "Process p = run.exec("java Test1")" by adding bin folder( right clikcing JavaTest(project name)->Run As->Run Configuration | Tab Classpath --- User Entries --- Advanced --- Add Folders ), then Test1.txt is not created by new codes. So where is the problem ?
To me program seemed unnecessarily complex. Why not below(if you dont have specific requirement)
import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
try {
Test1.createFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void createFile()
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Categories

Resources