Error while trying to read an Excel file - java

When I run the code below:
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Read object repository file
InputStream stream = new FileInputStream(new File(System.getProperty("D:\\src\\objects\\object.properties")));
//load all objects
p.load(stream);
return p;
}
It shows error as:
Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at uioperation.Excel_object.getObjectRepository(Excel_object.java:14)
at ExecuteTestcase.Testcase_execute.main(Testcase_execute.java:27)
What is wrong with my code?

I do not believe that you have a System property called D:\\src\\objects\\object.properties
so
change to
new File("D:\\src\\objects\\object.properties"));
assuming that this file does exist

Seems your code for setting the property is not implemented, But you can simply Open a file using following code.
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Read object repository file
String FileName="Path to your file";
InputStream stream = new FileInputStream(new File(fileName));
//load all objects
p.load(stream);
return p;
}
Or otherwise, you should get it from system properties..
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Read object repository file
String FileName=System.getProperty("Prperty Key for your file path"); // the property should ave been already set somewhere in the code before execution of this line
InputStream stream = new FileInputStream(new File(fileName));
//load all objects
p.load(stream);
return p;
}
This is for setting the property and accessing the file
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Set Property for file path
setProperty("filePath","D:\\src\\objects\\object.properties");
//Read object repository file
String FileName=System.getProperty("filePath"); //now fileName is similar to "D:\\src\\objects\\object.properties"
InputStream stream = new FileInputStream(new File(fileName));
//load all objects
p.load(stream);
return p;
}

Related

Java: How to convert com.google.api.services.drive.model.File to InputStream?

I am new to google drive integration.
I saved an image in google drive and I got that file by below method.
File file = getDriveService().files().get(fileId).execute();
Now when I tried to convert this file into InputStream and write this InputStream in HttpResponse then the file returns but the image is not displayed.
public InputStream convertGoolgeFileToInputStream( String fileId ) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
getDriveService().files().get(fileId).executeAndDownloadTo(outputStream);
InputStream in = new ByteArrayInputStream(outputStream.toByteArray());
return in;
//return getDriveService().files().get(fileId).executeAsInputStream();
}
You may want to try checking the download example Drive sample
private static void downloadFile(boolean useDirectDownload, File uploadedFile)
throws IOException {
// create parent directory (if necessary)
java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Unable to create parent directory");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
MediaHttpDownloader downloader =
new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
downloader.setDirectDownloadEnabled(useDirectDownload);
downloader.setProgressListener(new FileDownloadProgressListener());
downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}

How can I read a path from an .ini file

I would like to import a file path in Java. Since the path can change, I want it to be outside of the code and so it is changeable. I have read that that can solve with an INI file. Well, I've tried it. I have the following Java code:
import java.util.*;
import java.io.*;
class readIni {
public static void main(String args[]) {
readIni ini = new readIni();
ini.doit();
}
public void doit() {
try{
Properties p = new Properties();
p.load(new FileInputStream("user.ini"));
p.list(System.out);
}
catch (Exception e) {
System.out.println(e);
}
}
}
My Ini-file:
file = H:/
Now, the console shows exactly the Ini-file and not the contents of the directory....What is wrong?
If you want to just save a file path, consider using the following code:
File file = new File("H:\whatever.txt");
// Write to the file
FileWriter fw = new FileWriter(file);
fw.write("your path goes here");
fw.close();
// Read from the file
BufferedReader br = new BufferedReader(new FileReader(file));
String path = br.readLine();
br.close();

create a temporary file with a specified name in java

I have a Byte[] array that i want to put it's content into a temporary file .
I have tryied to do it like this
try {
tempFile = File.createTempFile("tmp", null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(sCourrier.getBody());
} catch (IOException e) {
e.printStackTrace();
}
but i want that I specify the filename by myself so not generated by the jvm
You can directly give the location and file name or You can access local filesystem and find the temp directory
String tempDir=System.getProperty("java.io.tmpdir");
you can use temp directory and your custom file name.
public static void main(String[] args) {
try {
String tempDir=System.getProperty("java.io.tmpdir");
String sCourrier ="sahu";
File file = new File(tempDir+"newfile.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(sCourrier.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
You can use Guava Files.createTempDir():
File file = new File(Files.createTempDir(), fileName.txt);
But because the API is deprecated and they also recommend to use Nio with more params:
Path createTempDirectory(String prefix, FileAttribute<?>... attrs)
so it would be better if you have a method yourself:
File createTempFile(String fileName, String content) throws IOException {
String dir = System.getProperty("java.io.tmpdir");
File file = new File(dir + fileName);
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(content.getBytes(StandardCharsets.UTF_8));
}
return file;
}

Programmatically reading contents of text file stored in HDFS using Java

How do I run this simple Java program to read bytes from a text file stored in directory/words in HDFS? Do I need to create a jar file for the purpose?
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.hadoop.*;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class filesystemhdfs
{
public static void main(String args[]) throws MalformedURLException, IOException
{
byte[] b=null;
InputStream in=null;
in=new URL("hdfs://localhost/words/file").openStream();
in.read(b);
System.out.println(""+b);
for(int i=0;i<b.length;i++)
{
System.out.println("b[i]=%d"+b[i]);
System.out.println(""+(char)b[i]);
}
}
}
You can use the HDFS API, this can be run from local.:
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://namenode:8020");
FileSystem fs = FileSystem.get(configuration);
Path filePath = new Path(
"hdfs://namenode:8020/PATH");
FSDataInputStream fsDataInputStream = fs.open(filePath);
First, you need to tell the JVM about the HDFS scheme in the URLs objects. This is done via:
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
After compiling your Java class, you need to use hadoop command:
hadoop filesystemhdfs
Hadoop comes with a convenient IOUtils. It will ease a lot of stuff for you.
You can not read a file from HDFS , as a regular filesystem java supports. You need to use HDFS java API for this.
public static void main(String a[]) {
UserGroupInformation ugi
= UserGroupInformation.createRemoteUser("root");
try {
ugi.doAs(new PrivilegedExceptionAction<Void>() {
public Void run() throws Exception {
Configuration conf = new Configuration();
//fs.default.name should match the corresponding value
// in your core-site.xml in hadoop cluster
conf.set("fs.default.name","hdfs://hostname:9000");
conf.set("hadoop.job.ugi", "root");
readFile("words/file",conf)
return null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readFile(String file,Configuration conf) throws IOException {
FileSystem fileSystem = FileSystem.get(conf);
Path path = new Path(file);
if (!ifExists(path)) {
System.out.println("File " + file + " does not exists");
return;
}
FSDataInputStream in = fileSystem.open(path);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = br.readLine())!= null){
System.out.println(line);
}
in.close();
br.close();
fileSystem.close();
}
public static boolean ifExists(Path source) throws IOException {
FileSystem hdfs = FileSystem.get(conf);
boolean isExists = hdfs.exists(source);
System.out.println(isExists);
return isExists;
}
Here I am trying from a remote machine, that's why I am using UserGroupInformation and write code in the run method of PrivilegedExceptionAction . If you are in the local system you may not need it. HTH!
Its a bit late to reply, but it will help future reader. It will iterate your HDFS directory and will read the content of each file.
Hadoop client and Java is used only.
Configuration conf = new Configuration();
conf.addResource(new Path(“/your/hadoop/conf/core-site.xml"));
conf.addResource(new Path("/your/hadoop/confhdfs-site.xml"));
FileSystem fs = FileSystem.get(conf);
FileStatus[] status = fs.listStatus(new Path("hdfs://path/to/your/hdfs/directory”);
for (int i = 0; i < status.length; i++) {
FSDataInputStream inputStream = fs.open(status[i].getPath());
String content = IOUtils.toString(inputStream, "UTF-8");
}

IOException at java.io.File.createNewFile();

I do have some code for serialization which does not work. I have tried to insert both CanRead() and CanWrite() in if-statements which showed they did not have permissions to read and write. I have also tried to insert 'java.io.File.setReadable' and 'java.io.File.setWriteable to true but it still throws the error.
The code is as following:
public static void save(Object obj, String filename) throws FileNotFoundException, IOException
{
File f = new File("c:/DatoChecker/" + filename);
File dir = new File("c:/DatoChecker");
if(!dir.exists())
dir.mkdirs();
f.setReadable(true);
f.setWritable(true);
if(!f.exists())
{
f.createNewFile();
}
FileOutputStream op = null;
ObjectOutputStream objectStream = null;
op = new FileOutputStream(f);
objectStream = new ObjectOutputStream(op);
objectStream.writeObject(obj);
objectStream.close();
}
public static Object fetch(String filename) throws FileNotFoundException, IOException, ClassNotFoundException
{
File f = new File("c:/DatoChecker" + filename);
File dir = new File("c:/DatoChecker");
if(!dir.exists())
dir.mkdirs();
f.setReadable(true);
f.setWritable(true);
if(!f.exists())
{
f.createNewFile();
}
FileInputStream ip = null;
ObjectInputStream objectStream = null;
Object obj = null;
ip = new FileInputStream(f);
objectStream = new ObjectInputStream(ip);
obj = objectStream.readObject();
ip.close();
objectStream.close();
return obj;
}
Stacktrace:
SEVERE: null
java.io.IOException: access denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:947)
at com.check.me.Serialization.fetch(Seralization.java:39)
at com.check.me.GoodsList.load(GoodsList.java:82)
at com.check.me.START.main(START.java:22)
The one for save is congruet from GoodsList (just save instead of load) and up but it is quite a bit longer below so I will leave it out for now.
Thanks for the help beforehand
Highace2
You state that you did not have permission to read or write. And, indeed, you get an error telling you that you don't have permission. You need to change the ACL on the directory in which you are creating the file, or pick a different directory.

Categories