I want to read and write a text document which i upload in "one drive" or "google drive" in java.
BufferedReader bufferedReader;
String line;
URL url = new URL("https://docs.google.com/document/d/1JLRmW7eaXHyyoPdNCyNN7AHWzKWaAPWgL84sxHUB7dQ/edit");
bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
With this code i only read the HTML documentation and thats not helpful.
In this text are some IPs from servers I have to connect with clients.
Note that i want to access this file from a java program, not an android app.
As i researched, the available google drive apis are not quite suited for java implementation,
since they are build for android solutions.
the available google drive apis are not quite suited for java implementation
Google APIs support Java. Android is Java, so the API (classes, methods you need to call, etc.) is the same on either platform.
The Drive API is part of the Google API Client Library. Here is a great overview:
Google API Client Library
A Developer's Guide with instructions on getting set up and some Examples.
The drive-cmdline-sample project would be a good place to start. There is a downloadFile method that looks like it will get the source file:
/** Downloads a file using either resumable or direct media download. */
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);
}
Related
I am trying to read file from the Local storage inside the Project of an Android.
However I am still having error like file not found exception. I had print the path of the file and I have checked with the browser file is there on the same path. but I am having still exception.
try {
File file = new File("D:\\Android SDK\\AndroidWorkspace\\Assignment4Test\\app\\src\\main\\assets\\studentnameid.txt");
if (file.exists()) {
System.out.println("File Is there ");
} else {
//It always executes the else blog.
file.createNewFile();
System.out.println(" file is not there so its creating Created File");
}
System.out.println(file.getAbsolutePath());
StringBuilder text = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
I would appreciate your help.
Thanks,
Krishna
D:\\Android SDK\\AndroidWorkspace\\Assignment4Test\\app\\src\\main\\assets\\studentnameid.txt is a path to a file on a Windows PC. Android is not Windows. You do not use Windows filesystem paths to refer to anything on Android.
My guess, based on that path, is that you have a file in your project in assets/. If so, use AssetManager (via getAssets() on a Context) and its open() method to access your asset. That will give you an InputStream to read from, directly or via an InputStreamReader.
to simplify this you have to use a localhost WAMP (for Windows ) or MAMP(for Mac), then store your file in the (www) folder, Go to android studio and set the link of your file like ( http://192.168.1.100/studentnameid.txt)
the 192.168.1.100 is your local IP address.
Not: you must change your IP address by the domaine name when you publish your app in Google play or other platforms
what i tried
try {
File fileDir = new File("B:\\Palringo\\palringo.exe");
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream("B:\\Palringo\\palringo.exe"), "UTF8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
Output :
unreadable Strings
what i want
i want to control the (palringo.exe) so i can make Bot for it
What is palringo.exe ?:
its a chatting program you can download it or use web version (palringo.im).
am i doing wrong by opening a file that is exe ? should i connect to the website by Connection classes in java ? if so , how i can connect it ?
This doesn't work. You cannot read an exe file.
You need to have the source code or library to add that software to you code. You simply cannot read a exe file and extract code, because exe file will be encrypted and it will be in lower level languages.
But you can use exec() to run that exe file.
I know this is a very late answer, if you are looking to connect and manipulate palringo, there are a couple of APIs available.
https://github.com/calico-crusade/PalringoApi
This specific one can also be found on Nuget, though it is for C#. You could copy over the majority of the connection code to Java if you wish.
I have an Android project that displays data from a JSON file. The file is read from the assets directory, following the approach below:
src/main/assets/my_file.json
InputStream is = getResources().getAssets().open(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
// use StringBuilder to get file contents as String
I also have local unit tests which require the same JSON file. In order to avoid the slowness of Instrumentation tests, I am currently loading a duplicate of the file as below:
src/main/resources/my_copy.json
File testData = new File(getClass().getResource(filename).getPath());
InputStream is = new FileInputStream(testData);
// read File as before
Is there an approach that would allow the JSON file to be stored in one location, whilst running the unit tests on a local JVM?
If you're using Robolectric, see this answer.
Note that the "assets" directory is an Android concept, it's different from Java resources. That said, you could also move your JSON file from assets to resources, and use it from both Android and JVM form there like you would in any Java application.
Files in the resource directory can be accessed within Android applications by using ClassLoader#getResourceAsStream(). This returns an InputStream, which can then be used to read the file. This avoids having to duplicate files between resources and the assets directory.
InputStream is = getClass().getClassLoader().getResourceAsStream("my_file.json");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
}
String json = sb.toString();
I am trying to create a java console app which will download a file from a URL. The file is created at Runtime and I don't know the file name. When I copy and paste the URL in the browser the save file pop up comes up to save the file. Now I want to create Java code which logs on to the server (validates user I have that done) go to that URL and download the file.
Sergii's answer is a good start; however, if the website you want to use requires more that a simple download, consider using Apache's HttpClient.
It supports cookies, authentication, URIs as well as URLs, and file upload.
Simple exapmple from docs, just to put your url insted of http://www.oracle.com/, and change System.out.println(inputLine); to write file on disk.
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
I have a file copied in one computer and I need to access the file from other computer.
I am not sure, which protocol or which technology to use for this?
Please provide me any hints for this..
Update:
I am using Ubuntu Linux system.
I used the code :
File f = new File("//192.168.1.157/home/renjith/picture.jpg");// 192.168.1.157 is the ip of the computer, where I have the picture file
Image image = ImageIO.read(f);
But it is giving an exception:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1275)
I have shared renjith folder also.
There are any number of ways to access files on remote machines, but they virtually all depend on the remote machine having been set up to provide the file in some way first. If you with to access files via java, the easiest method would probably be to set up an HTTP server on the remote machine (this can be done pretty easily using Apache HTTP server on a variety of platforms) and then using Apache Commons HTTPClient on the client side java app. Further discussion of how to install these or configure them is generally beyond the scope of Stack Overflow and would at least require a more specific question
HTTP is an option. However, if these are Windows machines on the same LAN, it would be easier to expose the directory on the remote machine via a file share and access the file through a regular file path. Similarly, if these are Unix-like machines, you could use regular file paths if you're using NFS. FTP's yet another option.
if the remote computer is in the same network and on a shared folder to the computer where your java code is running then try this piece of code for accessing it
File file = new File("\\\\Comp-1\\FileIO\\Stop.txt");
here Comp-1 is the DNS name of the machine containing the file in the network!!!
You might try:
URL url = new URL("file://192.168.1.157/home/renjith/picture.jpg");
Image image = ImageIO.read(url);
You could try to mount that path first, and then load it. Do a :
subst x: \\192.168.1.157
and then:
File f = new File("x:\\home\\renjith\\picture.jpg");
Image image = ImageIO.read(f)
It should work.
Share the directory and access the file thruogh java code
try this one:
File f = new File("//10.22.33.122/images")
File[] files = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
// Specify the extentions of files to be included.
return name.endsWith(".bmp") || name.endsWith(".gif");
}
});
// get names of the files
String[] fileNamesArray = null;
for (int indx = 0; indx < files.length(); indx++) {
fileNamesArray[indx] = files[indx].getName();
}
return fileNamesArray;
Map your IP to network drive and try let us say the drive letter is X,
then code changes to File f = new File("x:\\home\\renjith\\picture.jpg");
Infact your file is already loaded in object f , try priting the value of the path f.getAbsolutePath() to console and see.. Actual error is with ImageIO
You can read from remote and write to remote using jcifs-1.3.15.jar jar in java but first you need to share location from remote system then it's possible.
try{
String strLine="";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("WORKGROUP", "username", "passwd"); // Authentication info here, domain can be null
// try (InputStream is = new SmbFile("smb://DESKTOP-0xxxx/usr/local/cache/abc.txt", auth).getInputStream()) {
try (InputStream is = new SmbFile("smb://xx.xx.xx.xxx/dina_share/abc.txt", auth).getInputStream()) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
}
} catch (IOException e) {
e.printStackTrace();
}
String smbURL="smb://xx.xx.xx.xxx/dina_share/abcOther.txt";
SmbFileOutputStream fos = new SmbFileOutputStream(new SmbFile(smbURL,auth));
byte bytes[]="Wellcome to you".getBytes();
fos.write(bytes);
}catch(Exception e){
e.printStackTrace();
}