How to write in specific place inside a file using ant - java

i am using java ant to deploy my application . I have a file app.php . I want to write some text in app.php while deploying in a specific location inside that file . This is my app.php :
'providers' => array(
'Illuminate\Validation\ValidationServiceProvider',
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',
),
I want to add a line at the end of this line :
'Illuminate\Workbench\WorkbenchServiceProvider',
Please tell me how to do this .
Thanks.

you must use subString method like this:
at first store your file in a String so after that you could do this:
String s="your file";
String firstPart=s.substring(0,s.lastIndexOf(")")+1);
String lastPart=s.substring(s.lastIndexOf(")")+1);
firstPart=firstPart+"\n"+"'Illuminate\\Workbench\\WorkbenchServiceProvider',";
s=firstPart+lastPart;
How to read from file ?
Use BufferedReader to wrap a FileReader
BufferedReader br = null;
StringBuilder sb=new StringBuilder();
try {
String line;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((line= br.readLine()) != null) {
sb.append(line+"\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
String str=sb.toString();

See the Replace or Filter ant tasks.

Related

Convert string read from file to JSONObject android

I have created a JSONObject and put values in it like below .
Then I converted my object "h" to string and write a file in the sdcard with that string.
JSONObject h = new JSONObject();
try {
h.put("NAme","Yasin Arefin");
h.put("Profession","Student");
} catch (JSONException e) {
e.printStackTrace();
}
String k =h.toString();
writeToFile(k);
In the file I see text written like the format below .
{"NAme":Yasin Arefin","Profession":"Student"}
My question is how do I read that particular file and convert those text back to JSONObject ?
To read a file you have 2 options :
Read with a BufferReader and your code would look like this :
//Path to sdcard
File sdcard = Environment.getExternalStorageDirectory();
//Load the file
File file = new File(sdcard,"file.json");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
The option 2 would be to use a library such as Okio:
In your Gradle file add the library
implementation 'com.squareup.okio:okio:2.2.0'
Then in your activity:
StringBuilder text = new StringBuilder();
try (BufferedSource source = Okio.buffer(Okio.source(file))) {
for (String line; (line = source.readUtf8Line()) != null; ) {
text.append(line);
text.append('\n');
}
}

Java Filenotfound exception in jar file

i wrote a program, but when my friends try to execute it it throw filenotfound exception, but the file is exist, here is my code, and in the folder have lib folder, the jar file and the "csv fajlok" and in the csv fajlok folder there is the 2 csv file
String csvFile = "csv fajlok\\pontcsoport.csv";
BufferedReader br = null;
String line = "";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] pontGroupLine = line.split(";");
String[] price_split = pontGroupLine[1].split(" ");
try{
doubleDTList.add(Double.parseDouble(price_split[0]));
}catch(NumberFormatException e){
}
}
}catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Nem található a pontcsoport fájl (/csv fajlok)");
}catch (IOException e) {
}finally {
if (br != null) {
try {
br.close();
}catch (IOException e) {
}
}
}
If the file is outside the jar file, you should put an absolute path, or the "csv fajlok" folder should be in the same folder where you execute the jar file.
If the file is inside the jar file, you cannot access to it as a file but as a Stream, with the method Class.getResourceAsStream(String path).
Better to use Java NIO2 to read the file content:
List<String> lines = Files.readAllLines(Paths.get("path-to-file"), charset);
It's allow to avoid using while loop with redundant readers.
What is the OS installed on failed notebooks?
Also try to change folder name using '_' instead of space. I think it's the main reason of the issue.

Location of text files in android project

I want to read and write some data from text ( .txt ) files in my android project. ( The files are going to be included in the project itself for storing some user preference data )
I want to use only PURE JAVA PROVIDED METHODS to do that, like -
Scanner myScanner = new Scanner( new File( "file_name.txt" ) );.
These are a few static data in a non-activity class. So i don't have any Context to use for calling getResources() or using SharedPreferences. And there is no way i can pass a context from elsewhere to the class. I'm not going to explain why cause it's going to be a long story. Please don't give any suggestions regarding these ways.
My question is simple - if I want to read/write files with java methods, exactly where do i need to put them in my project?
I AM USING ANDROID STUDIO.
I start with reading a simple text.txt, you first have to upload a file in an asset folder after that you create a Private String method
private String readFileFromAssets(String filename) {
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(
getAssets().open(filename)));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
return builder.toString();
} catch (FileNotFoundException e) {
displayMessage("File not found: " + e.getMessage());
} catch (Exception e) {
displayMessage(e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
displayMessage(e.getMessage());
}
}
}
return null;
}
give me a feedback about it.

Importing a text file in Android SDK

I've been trying to read a file for the last few days and have tried following other answers but have not succeeded. This is the code I currently have to import the text file:
public ArrayList<String> crteDict() {
try {
BufferedReader br = new BufferedReader
(new FileReader("/program/res/raw/levels.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] linewrds = line.split(" ");
words.add(linewrds[0].toLowerCase());
// process the line.
}
br.close();
}
catch (FileNotFoundException fe){
fe.printStackTrace();
It is meant to read the text file and just create a long Array of words. It keeps ending up in the FileNotFoundException.
Please let me know any answers.
Thanks!
IF your file is stored in the res/raw folder of the android project, you can read it as follows, this code must be inside an Activity class, as this.getResources() refers to Context.getResources():
// The InputStream opens the resourceId and sends it to the buffer
InputStream is = this.getResources().openRawResource(R.raw.levels);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
// While the BufferedReader readLine is not null
while ((readLine = br.readLine()) != null) {
Log.d("TEXT", readLine);
}
// Close the InputStream and BufferedReader
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}

BufferedReader method ReadLine() is converting en-dash ("\u2013") to hyphen ("\u002D")

I have folders in a repository in SVN which have an en-dash ("\u2013") in their names.
I am first calling the "svn list" (in my Windows 7 + UTF-8 encoding) to get the list of the directory.
After that calling BufferedReader readLine(), it reads the text of the list.
The name of the folders being displayed contain a hyphen ("\u002D") instead of the en-dash ("\u2013").
Are there any limitations regarding that ?
class Test {
public static void main(String args[]) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\test–ing.xml"));
System.out.println(br.readLine());
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} // end main
This is probably the problem:
br = new BufferedReader(new FileReader("C:\\test–ing.xml"));
That will use the platform default encoding. You've said that the file is UTF-8-encoded - so you need to specify that you want UTF-8, which means avoiding FileReader's broken API:
br = new BufferedReader(new InputStreamReader(
new FileInputStream("C:\\test–ing.xml"), "UTF-8"));
That's assuming the file really is valid UTF-8 containing the expected character. You should check that before doing anything else.
Alternatively, given that this is XML, I assume in your real code you're going to use it as XML? If so, I would just load it straight from an input stream, and let the XML parser handle the encoding.

Categories

Resources