What are the steps to opening a .dat file in java? - file

I have another assignment which is obviously giving me trouble.
I need to write code that will open a .dat file from my hard drive in Netbeans IDE.
The file doesn't contain binary data just simple text characters.
When I run the code I've written it gives an error...because it cannot locate the file. Any Suggestions
package countedcones;
import java.io.*;
public class CountedCones {
public static void main(String[]args) throws IOException{
FileReader in = new FileReader("icecream.dat");
BufferedReader br = new BufferedReader(in);
String line = br.readLine();
while (line!=null) {
System.out.println(line);
line = br.readLine();
}
in.close();
}
}

Related

base64 to file in java6 with data longer than 65000

I've Base64 code that i need to open it as file , i used DatatypeConverter.parseBase64Binary which is working fine with data less than 65000 , but the issue that i receive codes which is longer than that how can i manage this in JAVA6 ???
Below is the Code i'm using , which is working fine with code less than 65000
CREATE OR REPLACE JAVA SOURCE NAMED "BlobHandler" AS
import java.lang.*;
import java.sql.*;
import oracle.sql.*;
import java.io.*;
import javax.xml.bind.DatatypeConverter;
public class BlobHandler
{
public static void ExportBlob(String CONTFILE, BLOB CONTBLOB) throws Exception
{
try
{
File file = new File("/u01/oracle/jam_export/contract1");
FileOutputStream fos = new FileOutputStream(file);
//Decoding
byte[] decoded = DatatypeConverter.parseBase64Binary("JVBERi0xLjcNCiW1");
System.out.println("PDF File Saved");
fos.write(decoded);
fos.flush();
fos.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
};
Note DatatypeConverter.parseBase64Binary("JVBERi0xLjcNCiW1"); is a part of the BASE64 code for the post character limitation

What is the use of System.getProperty() while taking Screenshot in Selenium?

I'm running a program for adding screenshot in extent report automatically using selenium. Program is running perfectly,but I want to know the meaning of System.getProperty line in below program .
public class SST
{
public static String getScreenshot(WebDriver driver)
{
TakesScreenshot ts=(TakesScreenshot) driver;
File src=ts.getScreenshotAs(OutputType.FILE);
String path = System.getProperty("user.dir")+"/Screenshot/"+System.currentTimeMillis()+".png";
File destination=new File(path);
try
{
FileUtils.copyFile(src, destination);
} catch (IOException e)
{
System.out.println("Capture Failed "+e.getMessage());
}
return path;
}
}
It is getting the user home directory, for example, C:\Users\user10796675.

Hadoop Map Reduce - Read HDFS File - FileAlreadyExists error

I am new to Hadoop. I am trying to read an existing file on HDFS using the below code. The configuration seem file and the file path is correct as well. -
public static class Map extends Mapper<LongWritable, Text, Text, Text> {
private static Text f1, f2, hdfsfilepath;
private static HashMap<String, ArrayList<String>> friendsData = new HashMap<>();
public void setup(Context context) throws IOException {
Configuration conf = context.getConfiguration();
Path path = new Path("hdfs://cshadoop1" + conf.get("hdfsfilepath"));
FileSystem fs = FileSystem.get(path.toUri(), conf);
if (fs.exists(path)) {
BufferedReader br = new BufferedReader(
new InputStreamReader(fs.open(path)));
String line;
line = br.readLine();
while (line != null) {
StringTokenizer str = new StringTokenizer(line, ",");
String friend = str.nextToken();
ArrayList<String> friendDetails = new ArrayList<>();
while (str.hasMoreTokens()) {
friendDetails.add(str.nextToken());
}
friendsData.put(friend, friendDetails);
}
}
}
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
for (String k : friendsData.keySet()) {
context.write(new Text(k), new Text(friendsData.get(k).toString()));
}
}
}
I am getting the below exception when I run the code -
Exception in thread "main" org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://cshadoop1/socNetData/userdata/userdata.txt already exists
at org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.checkOutputSpecs(FileOutputFormat.java:146)
at org.apache.hadoop.mapreduce.JobSubmitter.checkSpecs(JobSubmitter.java:458)
at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:343)
I am just trying to read an existing file. Any ideas what I am missing here? Appreciate any help.
Exception tells you that your output directory already exists but it should not. Delete it or change its name.
Moreover the name of your output directory 'userdata.txt' looks like the name of a file. So check you are not mistaken in your input/output directories.

How to read objects of arrays from a .txt file?

First what i have done is write an ArrayList as ann object to a .txt file, what I am trying to do is read back those objects into an array and display the array(reverse of first step). The arraylist i'm passing to the funnctions are salesman and technicians that extend employees with parameters name,number,target,terriorty etc. I get this error when i try to compile it.
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to Employee
at FileIO.readObject(FileIO.java:165)
at UseCompany.main(UseCompany.java:61)
Trying to read back objects from text file
ArrayList<Employee> use_company_arraylist2 = FileIO.("C:\\Users\\Saad\\Documents\\writeObjectMethod.txt"");
use_company_arraylist2 = FileIO.readObject("C:\\Users\\Saad\\Documents\\writeObjectMethod.txt");
The writing object to text file code:
public static void writeObject(ArrayList<Employee> array_of_employee, String filename)
{
try{
//create file stream and write array to file using stream using objectoutput stream
FileOutputStream fle = new FileOutputStream("c:\\Users\\Saad\\Documents\\writeObjectMethod.txt");
ObjectOutputStream oos = new ObjectOutputStream(fle);
oos.writeObject(array_of_employee);
oos.close();
fle.close();
}
catch(FileNotFoundException filenotfound)
{
System.out.println("FILE NOTE FOUND");
}
catch(IOException ioerror)
{
System.out.println("input or output error");
}
}//end writeObject
The reading object from text file code:
public static ArrayList<Employee> readObject (String filename)
{
ArrayList<Employee> newArrayList = new ArrayList<Employee>();
try
{
FileInputStream readfle = new FileInputStream(filename);
ObjectInputStream readobjectfile = new ObjectInputStream(new BufferedInputStream(readfle));
newArrayList.add((Employee)readobjectfile.readObject());
}
catch(ClassNotFoundException clasnfd)
{
System.out.println("class error?");
}
catch(IOException ioerror)
{
System.out.println("input or output error");
}
return newArrayList;
}//end readObject
An object of type Arraylist can't be casted to an object of Employee class.
This will work
ArrayList list = new ArrayList();
list.add(new Employee());
// for writing arraylist object to file
new ObjectOutputStream(new FileOutputStream(new File("a.txt"))).writeObject(list);
// for reading array list from file
ArrayList<Employee> anotherList = (ArrayList<Employee>) new ObjectInputStream(new FileInputStream(new File("a.txt"))).readObject();
System.out.println(anotherList.size());

Cannot Find Symbol for another class file

I've had this problem a few times, where I've created another class file and the main class file can't find it.
Here's the main class file:
package textfiles;
import java.io.IOException;
public class FileData
{
public static void main(String[] args)
{
String file_name = "Lines.txt";
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
for(int i =0; i<aryLines.length; i++)
{
System.out.println(aryLines);
}
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
Here is the class file it can't find:
package textfiles;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile
{
private String path;
int numberOfLines=0;
public ReadFile(String file_path)
{
path = file_path;
}
public String[] OpenFile() throws IOException
{
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for(int i=0; i<numberOfLines; i++)
{
textData[i] = br.readLine();
}
br.close();
return textData;
}
int readLines() throws IOException
{
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
while((aLine = bf.readLine()) != null)
{
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}
I've tried running javac textfiles\ReadFile.java and javac textfiles\FileData.java as a suggestion for this. That doesn't work. I've made sure I have compiled ReadFile and fixed all the errors there.
The compiler error I get is:
C:\Users\Liloka\Source>javac FileData.java
FileData.java:13: cannot find symbol
symbol : class ReadFile
location: class textfiles.FileData
ReadFile file = new ReadFile(file_name);
^
FileData.java:13: cannot find symbol
symbol : class ReadFile
location: class textfiles.FileData
ReadFile file = new ReadFile(file_name);
^
2 errors
I'm using notepad++and .cmd so it can't be an IDE error.
Thanks in advance!
Make sure the java files are all in the textfiles directory:
textfiles/FileData.java
textfiles/ReadFile.java
And run:
javac textfiles/FileData.java textfiles/ReadFile.java
java textfiles.FileData
Your code works without any modification. I think you are compiling from a wrong directory:
C:\Users\Liloka\Source>javac FileData.java
Move the FileData.java to the textfiles directory.
You have to compile all the java files used by your main class. As ReadFile is used by FileData you have to compile it too.
Did you tried
javac Filedata.java ReadFile.java
or
javac *.java
?
There must be a conflict with generated classes.
Just try to remove all the classes that have been generated and build project again.

Resources